SharePoint change document set and items content type to a new content type

I’ll put it simply:

This is a PowerShell script that you can use to change a content type of a library or list to another one. This script can identify between Document Sets and library or list items.

param (
[string]$WebsiteUrl = "",
[string]$OldCTName = "DSTestCT",
[string]$NewCTName = "DSTestCT"

if ( (Get-PSSnapin -Name MicroSoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
Add-PsSnapin MicroSoft.SharePoint.PowerShell

function Reset-ListContentType ($WebUrl, $ListName, $OldCTName, $NewCTName)
$web = $null
$web = Get-SPWeb $WebUrl

$list = $web.Lists.TryGetList($ListName)
$oldCT = $list.ContentTypes[$OldCTName]

$isChildOfCT = $list.ContentTypes.BestMatch($rootNewCT.ID).IsChildOf($rootNewCT.ID);
if($oldCT -ne $null -and $isChildOfCT -eq $false)
$hasOldCT = $true
$isFoldersCTReseted = Reset-SPFolderContentType –web $web -list $list –OldCTName $OldCTName –NewCTName $NewCTName
Reset-SPFileContentType –web $web -list $list –OldCTName $OldCTName –NewCTName $NewCTName
Remove-ListContentType –web $web -list $list –OldCTName $OldCTName –NewCTName $NewCTName
if($hasOldCT -eq $true)
Add-ListContentType –web $web -list $list –OldCTName $OldCTName –NewCTName $NewCTName
if($isFoldersCTReseted -eq $true)
Set-SPFolderContentType –web $web -list $list –OldCTName $OldCTName –NewCTName $NewCTName




function Remove-ListContentType ($web, $list, $OldCTName, $NewCTName)

$oldCT = $list.ContentTypes[$OldCTName]

$isChildOfCT = $list.ContentTypes.BestMatch($oldCT.ID).IsChildOf($oldCT.ID);

if($isChildOfCT -eq $true)

return $isChildOfCT

function Add-ListContentType ($web, $list, $OldCTName, $NewCTName)



function Reset-SPFolderContentType ($web, $list, $OldCTName, $NewCTName)
#Get web, list and content type objects

$isFoldersCTReseted = $false

$isChildOfCT = $list.ContentTypes.BestMatch($rootNewCT.ID).IsChildOf($rootNewCT.ID);

$oldCT = $list.ContentTypes[$OldCTName]
$folderCT = $list.ContentTypes["Folder"]
$newCT = $rootNewCT

$newCTID = $newCT.ID

#Check if the values specified for the content types actually exist on the list
if (($oldCT -ne $null) -and ($newCT -ne $null))
$list.Folders | ForEach-Object {

if ($_.ContentType.ID.IsChildOf($rootNewCT.ID) -eq $false -and $_.ContentType.ID.IsChildOf($oldCT.ID) -eq $true -and $_.Folder.ProgID -eq "Sharepoint.DocumentSet")
Write-Host "Found a document set: " $_.Name "Processing document set"
$item = $list.GetItemById($_.ID);
$item["ContentTypeId"] = $folderCT.Id
$isFoldersCTReseted = $true


return $isFoldersCTReseted

function Set-SPFolderContentType ($web, $list, $OldCTName, $NewCTName)
#Get web, list and content type objects

$folderCT = $list.ContentTypes["Folder"]
$newCT = $list.ContentTypes[$NewCTName]

#Check if the values specified for the content types actually exist on the list
if (($newCT -ne $null))
$list.Folders | ForEach-Object {
if ($_.ContentType.ID.IsChildOf($newCT.ID) -eq $false -and $_.ContentType.ID.IsChildOf($folderCT.ID) -eq $true -and $_.Folder.ProgID -eq "Sharepoint.DocumentSet")
$item = $list.GetItemById($_.ID);
$item["ContentTypeId"] = $newCT.Id


function Reset-SPFileContentType ($web, $list, $OldCTName, $NewCTName)
#Get web, list and content type objects

$isChildOfCT = $list.ContentTypes.BestMatch($rootNewCT.ID).IsChildOf($rootNewCT.ID);

$oldCT = $list.ContentTypes[$OldCTName]
$folderCT = $list.ContentTypes["Folder"]
$newCT = $rootNewCT

$newCTID = $newCT.ID

#Check if the values specified for the content types actually exist on the list
if (($oldCT -ne $null) -and ($newCT -ne $null))
$list.Folders | ForEach-Object {
if ($_.ContentType.ID.IsChildOf($rootNewCT.ID) -eq $false -and $_.ContentType.ID.IsChildOf($oldCT.ID) -eq $true)
$_["ContentTypeId"] = $folderCT.Id
#Go through each item in the list
$list.Items | ForEach-Object {
Write-Host "Item present CT ID :" $_.ContentType.ID
Write-Host "CT ID To change from :" $oldCT.ID
Write-Host "NEW CT ID to change to:" $rootNewCT.ID

#Check if the item content type currently equals the old content type specified
if ($_.ContentType.ID.IsChildOf($rootNewCT.ID) -eq $false -and $_.ContentType.ID.IsChildOf($oldCT.ID) -eq $true)
#Check the check out status of the file
if ($_.File.CheckOutType -eq "None")
Change the content type association for the item
$item = $list.GetItemById($_.ID);
write-host "Resetting content type for file: " $_.Name "from: " $oldCT.Name "to: " $newCT.Name

$item["ContentTypeId"] = $newCTID
Write-Host "Item changed CT ID :" $item.ContentType.ID
$item.File.CheckIn("Content type changed to " + $newCT.Name, 1)
write-host "File" $_.Name "is checked out to" $_.File.CheckedOutByUser.ToString() "and cannot be modified"
write-host "File" $_.Name "is associated with the content type" $_.ContentType.Name "and shall not be modified"
write-host "One of the content types specified has not been attached to the list"$list.Title


$web = Get-SPWeb $WebsiteUrl
$rootWeb = $web.Site.RootWeb;
$rootNewCT = $rootWeb.AvailableContentTypes[$NewCTName]

Foreach ($list in $web.Lists) {
Write-Host $list.BaseType
if($list.Hidden -eq $false -and $list.BaseType -eq "DocumentLibrary")
Write-Host "Processing list: " $list.Title
Reset-ListContentType –WebUrl $WebsiteUrl –ListName $list.Title –OldCTName $OldCTName –NewCTName $NewCTName



C# Code Optimization – Methods and Techniques


These are my notes based on a online course I took on C# optimization. They might be usefull to someone elese also:

They are based on this Udemy course:

C# Code optimization


Optimization strategies. 2

Warning. 2

.NET Fundamentals. 3

Stack. 3

Heap. 4

Value types. 5

Value types options. 6

Reference type. 6

Boxing and unboxing. 6

Immutable String. 6

Common Intermediate Language. 7

Instructions. 8

Key points. 9

Basic optimizations. 9

Avoid boxing and unboxing. 9

Slow Collections and other data holders. 9

Fast Collections. 9

Summary on boxing and unboxing. 10

String concatenation. 11

Appending to a string. 11

Appending to a StringBuilder. 11

Summary. 11

Collections. 12

Collections in .NET. 12

Summary. 12

Arrays. 12

Exceptions. 12

For vs Foreach. 13

Pros and cons. 13

Summary. 13

How to use for and foreach. 13

Intermediate optimization. 13

Fast garbage collection. 13

GC Problems. 13

Implicit assumptions about GC. 14

Gen:0 optimizations. 14

Lifetime optimizations. 14

Size optimizations. 15

Summary. 16

Fast delegates. 17

Sample code – Using delegates. 17

Fast class factories. 18

Sample solution. 18

Advanced optimization. 19

Arrays on the stack. 19

Sample code. 19

Pointers. 19

Pointers in C#. 19

Summary. 20

Optimization strategies

· Rewrite algorithms to achieve same results with less code

· Avoid unnecessary instructions

· Use libraries specifically designed for the task at hand

· Reduce memory consumption as much as possible

· Avoid scenarios where the code is blocking and waiting for a slow resource


· Do not try to optimize in advance since you can not know how your application will evolve and behave.

o The exception: Perhaps when you know what you are doing and even then at certain specific things not the entire or most of the application. Like when you clearly know certain elements and things which might become problematic because of X things like huge data processing in a certain situation which is known to be present in the application.

o The aim is to write simple, clear and modular code. Leave optimization to the very end, when you reliably know where the bottlenecks are.

.NET Fundamentals


· Information is stack upon one another as stack frame containing method parameters, local variables, return address etc.

· Tracks methods calls

· Contains frames which hold parameters, return address and local variables for each method call

· A stack frame is removed when returning from a method. All local variables go out of scope at this point

· If you call too many methods, the stack will fill up completely and .NET throws a StackOverflow exception


· Code that uses integers on the stack is slightly faster than code that uses objects on the heap.

· The new keyword creates objects on the heap

· Code that uses objects on the heap is slightly slower than code using integers on the stack.

· When variables on the stack go out of scope, their corresponding objects on the heap are dereferenced, not destroyed. The .NET Framework will always postpone cleaning up dereferenced objects.

· The .NET Framework eventually starts a process called Garbage Collection and deallocates all dereferenced objects on the heap.

Value types

· Value types store their value directly

o the type and value is store at the same location in the stack

· Value types can exist on the stack and the heap

o A value type created with an object

· Value types are assigned by value

o Data/value is copied from one value to another

· Value types are compared by value

Value types options

· sbyte

· byte

· char

· short

· ushort

· int

· uint

· long

· long

· float

· double

· decimal

· bool

· enum

· struct

Reference type

· Reference types can be set to null

· Reference type store a reference to a value on the heap

· Reference types can exist on the stack and the heap

o In the heap within an object

· Reference types are assigned by reference

o You can have multiple references to an instance of the object

· Reference types are compared by reference

o Meaning they compare by address

Boxing and unboxing

· Boxing takes a value type on the stack and stores it as an object on the heap

· Boxing happens when you assign a value type to a variable, parameter, field or property of type object

· Unboxing unpacks a boxed object on the heap, and copies the value type inside back to the stack

· Unboxing happens when you cast an object value to a value type

· Boxing and unboxing negatively affect performance

Immutable String

· Strings are reference types, and immutable

o Instead of modifying the original data, a new data is created and that is passed back. The original string is left untouched.

· Strings behave as if they are value types. They are assigned and compared by value

· Strings are thread-safe

· Strings are fast

o Fast assignment: Copy a string by copying the reference

o Fast comparison: Compare two interned strings by comparing the reference

· Strings conserve memory

o Memory savings: Identical strings can be merged

Common Intermediate Language

Why two compilations with .NET?

· JIT compiler can optimise for specific CPU

· Creates portable code that runs on any platform

· Code can be verified before running

· Code can be annotated with attributes

· Disadvantages: Slightly slower than direct compilation


· ldc.i4.1

o Load the 4-byte signed integer constant 1 on the evaluation stack.

· ldloc.0

o Load the variable in location 0 on the evaluation stack. The other value on the stack is pushed down.

· add

o Add the top two numbers on the evaluation stack together, and replace them with the result

· stloc.0

o Remove the value at the top of the evaluation stack, and store it in location 0

· box: box the top value on the stack

· bne: branch if top 2 values on stack are not equal

· call: call a static member of a class

· callvirt: call an instance member of a class

· ldelem/stelem: load and store array elements

· newarr: create a new 1-dimensional array

· newobj: create a new object on the heap

· ret: return from method

· throw: throw an exception

· unbox: unbox the top reference on the stack

Key points

· JIT compilation optimizes for local hardware

· IL code is portable, can be verified and annotated

· IL uses local variable locations and an evaluation stack

· Built-in support for objects

· Built-in support for 1-dimensional arrays

Basic optimizations

· Easy to implement, may require only one line of code to change

Avoid boxing and unboxing

· Boxing and unboxing causes an overhead when active

· The difference can be multiple times slower performance when unboxing and boxing

o Part of the reason for the slowness is because moving data from the stack and head

Slow Collections and other data holders

· These are classes which use object arrays internally for storing data


· ArrayList

· CollectionBase

· DictionaryBase

· HashTable

· Queue

· SortedList

· Stack

· HybridDictionary

· ListDictionary

· NameObjectCollectionBase

· OrderedDictionary


· DataSet

· DataTable

· DataRow

Fast Collections


· Generic collections use typed arrays for storing data

· Dictionary

· HashSet

· LinkedList

· List

· Queue

· SortedDictionary

· SortedList

· SortedSet

· Stack


· DataReader reads data directly without casting to object

· DataReader


· Consider using one dimensional arrays when you know the array size

Summary on boxing and unboxing

· Casting object variables to value types introduces an UNBOX instruction in intermediate code

· Storing value types in object variables introduces a BOX instruction in intermediate code

· Avoid casting to and from object in mission critical code

· Avoid using non-generic collections in mission critical code

· Avoid using DataTables in mission critical code, but only if you perform many operations on the same data table object

String concatenation

Appending to a string

Each append to the string creates a new copy and leaves the dereferenced original on the heap.

Appending to a StringBuilder

Each append to the StringBuilder writes into available buffer space.


· if you’re adding strings together in your code, always use the StringBuilder class

· Try to avoid adding strings using the + operator

· If you add 4 strings or less, use regular string variables and the + operator for the best performance

· If you add 4 strings or more, or you don’t know the number of additions in advance, use a StringBuilder

· For ten thousand additions, the StringBuilder is more than 240 times faster than regular string addition.


Collections in .NET

· Collection classes in System.Collections

· Generic collections in System.Collections.Generic

· Typed arrays


· Always use the generic collection classes from the System.Collections.Generic namespace in mission critical code

o Example: List<int> => Uses internally a native integer array instead of object array. This way unnecessary boxing is avoided.

· Avoid the classes in the System.Collection namespace in mission critical code.

· If you have mission critical code, and the number of elements is known in advance, use an array.

o compilers have a native support for arrays

o Also the arrays are pre-initialized. Notice that you can pre-initialize to a certain size in collections also.

· If you have mission critical code, and the number of elements is not known in advance, use a generic list

· Avoid the classes in System.Collections


· If you only have 1 dimension of data, use 1-dimensional arrays for the best performance.

· If you have 2 dimensions of data, flatten the array.

o Access the data with the following formula:

§ int index = 3 * row + col;

· If this is not possible, consider using a jagged array.

· If there is no other option, use a 2-dimensional array


· Never use exceptions in mission-critical code

o This means throwing exception from your mission critical code

· Use exceptions for fatal conditions that require an abort.

o Mostly situations on resources which you have no power over such as external physical devices, memory errors etc

· Don’t put try-catch blocks in deeply nested code

o Such as low level API calls

· Never use catch(Exception) to catch all exceptions

· Do not use exceptions for non-critical conditions

o Example – Parsing: Instead of catching exceptions with parsing use the TryParse function instead of Parse which has extra exception handling

§ In other words check if something is the way you want it instead of relying on exceptions to check if something is valid

· Never use exceptions to control the flow of your program

For vs Foreach

Pros and cons


· Pro: fastest but requires an indexer

· Con: indexer needs all values loaded in memory


· Pro: works on any collection

· Pro: loads values on demand

· Con: slower because it requires an enumerator


How to use for and foreach

· Array: do not refactor code, not worth it.

· List<>: refactor foreach to for to get 1.6x improvement

· ArrayList: refactor foreach to for to get 2.8x improvement, but consider using List<> instead

Non-generic enumerators return the current value as an object. Do not use them for value types to avoid boxing and unboxing. Always use generic enumerators if possible.

For value type collections, enumerate over IEnumerable, not IEnumerable, to avoid boxing and unboxing

Intermediate optimization

Fast garbage collection

GC Problems

First problem

· A mark and sweep Garbage Collector has to mark all live objects on the heap during each collection cycle.

· This can lead to long program freezes while the collection is running

· Also: repeatedly marking the same objects over and over in each cycle is very inefficient

Solution: Generational Garbage Collector

Second problem

· During 2 successive garbage collection cycles a long-living object will be compacted twice and moved twice: 4 memcopy operations for a single object

· For long-lived very large objects these 4 memory copy operations impact performance

Solution: Small Object and Large Object heaps

Implicit assumptions about GC

· Objects are either short-lived or long-lived

· Short-lived objects will be allocated and discarded within a single collection cycle

· Objects that survive two collection cycles are long-lived

· 90% of all small objects are short-lived

· All large objects (85K+) are long-lived

· Do not go against these assumptions

Gen:0 optimizations

· The more objects in generation 0, the more work the Garbage Collector has to do. So:

· Limit the number of objects you create

· Allocate, use, and discard objects as quickly as possible

Sample code


StringBuilder s = new StringBuilder(); for (int i=0; i < 10000; i++) { s.Append(i.ToString() + “KB”); }:

ArrayList list = new ArrayList(); for (int i=0; i < 10000; i++) { list.Add(i); }

public static MyObject obj = new MyObject(); … // lots of other code … UseTheObject(obj);


StringBuilder s = new StringBuilder(); for (int i=0; i < 10000; i++) { s.Append(i); s.Append(“KB”); }

List<int> list = new List<int>(); for (int i=0; i < 10000; i++) { list.Add(i); }

MyObject obj = new MyObject(); UseTheObject(obj); obj = null;

Lifetime optimizations

The Garbage Collector assumes 90% of all small objects are short-lived, and all large objects are long-lived. So:

· Avoid large short-lived objects

· Avoid small long-lived objects

Kuva 1: The areas where you want your objects to be

You want your object to be:

· Shor-lived + Small objects

· Long-lived + Large Object

Reuse objects (Object pooling)


ArrayList list = new ArrayList(85190); UseTheList(list); … list = new ArrayList(85190); UseTheList(list);


ArrayList list = new ArrayList(85190); UseTheList(list); … list.Clear(); UseTheList(list);

Reallocate after use (refactor code)


ArrayList list = new ArrayList(85190); for (int i=0; i < 10000; i++) { list.Add(new Pair(i, i+1)); }


int[] list_1 = new int[85190]; int[] list_2 = new int[85190]; for (int i=0; i < 10000; i++) { list_1[i] = i; list_2[i] = i + 1; }

Size optimizations

The Garbage Collector assumes 90% of all small objects are short-lived, and all large objects are long-lived. So:

· Avoid large short-lived objects

· Avoid small long-lived objects

Split objects(reduce footprint)


int[] buffer = new int[32768]; for (int i=0; i < buffer.Length; i++) { buffer[i] = GetByte(i); }


byte[] buffer = new byte[32768]; for (int i=0; i < 10000; i++) { buffer[i] = GetByte(i); }

Merge objects(resize lists)


public static ArrayList list = new ArrayList(); … // lots of other code … UseTheList(list);


public static ArrayList list = new ArrayList(85190); … // lots of other code … UseTheList(list);


· The Garbage Collector uses a mark/sweep/compact cycle

· Small objects are allocated on the Small Object Heap (SOH), large objects on the Large Object Heap (LOH)

o A large object is defined by having a size of 85 KB

· The Small Object Heap (SOH) has 3 generations. Objects are allocated in gen 0 and progress towards gen 2.

o Gen 0 is collected frequently, gens 1 and 2 infrequently

· The LOH has only one gen and does not compact

· .NET assumes 90% of all small objects are short-lived

· .NET assumes all large objects are long-lived

· Limit the number of objects you create

· Allocate, use, and discard small objects as fast as possible

· Re-use large object

· Use only small short-lived, and large long-lived objects

· Increase lifetime or decrease size of large short-lived objects

· Decrease lifetime or increase size of small long-lived objects

Fast delegates

· Use delegates in your code where it’s convenient.

· Remove delegates from mission critical code sections for a 9% performance boost.

· Always avoid multicast delegates in mission critical code, they are more than twice as slow as unicast delegates.

Sample code – Using delegates

// declare delegate

public delegate void AddDelegate (int a, int b, out int result);

// set up first addition method

private static void Add1 (int a, int b, out int result)


result = a + b;


// set up second addition method

private static void Add2 (int a, int b, out int result)


result = a + b;


Manually using

Add1 (1234, 2345, out result);

Add2 (1234, 2345, out result);

Unicast using

AddDelegate add1 = Add1;

AddDelegate add2 = Add2;

add1 (1234, 2345, out result);

add2 (1234, 2345, out result);

Multicast using

AddDelegate multiAdd = Add1;

multiAdd += Add2;

multiAdd (1234, 2345, out result);

Tips – Do not do

public static void Main(object[] args) {

myFunc = () => { };

myFunc += GetDelegateOrMaybeNull();



Don’t do this! The code is twice as slow now. Always use unicast delegates and check for null

Fast class factories

A class factory is a special class that constructs other classes on demand, based on external configuration data.

A class factory is a class that constructs class instances on demand using external configuration information.

· The Activator class is 86 times slower than compiled code.

· Dynamic method delegates are 5 times slower than compiled code and 17 times faster than the Activator class.

Replace the Activator class with dynamic method delegates in your class factories to speed up your code by a factor of 17!

Sample solution

· 1. Check dictionary if the delegate has been created already

· 2. If so -> retrieve and call delegate

· 3. If not ->

o 4. create dynamic method and write newObj and ret instructions into it.

o 5. Wrap method in a delegate and store in dictionary

o 6. Call delegate to instantiate class


// a delegate to create the object

private delegate object ClassCreator ();

// dictionary to cache class creators

private static Dictionary<string, ClassCreator> ClassCreators = new Dictionary<string, ClassCreator> ();

private static ClassCreator GetClassCreator (string typeName)


// get delegate from dictionary

if (ClassCreators.ContainsKey (typeName))

return ClassCreators [typeName];

// get the default constructor of the type

Type t = Type.GetType (typeName);

ConstructorInfo ctor = t.GetConstructor (new Type[0]);

// create a new dynamic method that constructs and returns the type

string methodName = t.Name + “Ctor”;

DynamicMethod dm = new DynamicMethod (methodName, t, new Type[0], typeof(object).Module);

ILGenerator lgen = dm.GetILGenerator ();

lgen.Emit (OpCodes.Newobj, ctor);

lgen.Emit (OpCodes.Ret);

// add delegate to dictionary and return

ClassCreator creator = (ClassCreator)dm.CreateDelegate (typeof(ClassCreator));

ClassCreators.Add (typeName, creator);

// return a delegate to the method

return creator;


ClassCreator classCreator = GetClassCreator (typeName);

classCreator ();

Advanced optimization

Arrays on the stack

Don’t use stack-based arrays to improve performance!

· Unreliable benefits, sometimes slower than native arrays

· Limited stack space available for data

· Results in unsafe code

Only use for interfacing with unmanaged code.

Sample code



int* list = stackalloc int[elements];

for (int r = 0; r < repetitions; r++)


for (int i = 0; i < elements; i++)


list [i] = i;





Pointers in C#

· To aid interoperability with unmanaged code

· To support a C or C++ .NET compiler

· To make it easy to port pointer-based algorithms to C#

Always use pointers for image manipulation in .NET

Avoid using pointers in C#, unless:

· Your algorithm processes a large block of data (10MB+)

· Your algorithm processes the data sequentially

· Your algorithm reads or writes data at or very close to the current pointer address

Using pointers will give you a 25% performance gain!

Sample code

Unsafe Keyword

Use unsafe keyword to tell the compiler of code that is not managed by the GC:

unsafe { // code using pointers }

public unsafe void Method (int i) { // code using pointers }

Pointer declaration

Byte* p = null;

Generic form: Type*

Fixed keyword

The object q pinned on the heap, will not be moved by the garbage collector.

fixed (Byte* p = &q) { … }

Pointer operations

Byte b = *p;

Byte b = p[2];

p = p + 3;


· Use high-level methods to access data in unmanaged memory, to avoid unsafe code

· If you need to read or write large blocks of unmanaged data directly, then obtain a pointer to the data

· For image manipulation always use pointers, because the image data will be too large for any kind of high-level access

How to display all of the refinement options in SharePoint 2013 Search results

By default for some reason, I could not show all of the items, I could define how many items are shown and but not get rid of the “show more” links under the refinements.

This JavaScript snippet can help you. It will hide the “short list” containing a minimum of items to be displayed, then it will show the long list and finally it will hide the “show more” link.

 $( document ).ready(function() {


Disassembling PowerShell commands to see how they work

If you ever wonder how PowerShell commands operate, what they do when you call them or how you can find out with the following commands what DLL the command is using and you can open the DLL with a disassembler like ILSpy.

$commandDetailsObj=Get-Command “your PS command here”


That is it, simple as that:).



These are my notes on Xamarin Development.

Presently this post is mostly based on this Udemy Course:



Using Lists in iOS and Android. 1

iOS Lists. 1

Using the Table View controller. 4

Android Lists. 8

Populating the view(activity). 9

Creating a custom cell with custom style and layout. 9

Creating a custom adapter. 13

Navigation between pages in iOS and Android – Part 2. 15

Navigation between pages in Android. 15

Navigation between pages in iOS. 15

Navigate to the previous view controller. 16

Retrieve Images from iOS phone. 16

Sharing Code between Android, iOS or any other platform.. 17

iOS User Interface for different iOS devices using Constraints. 19

Tips and Tricks. 21

iOS. 21

Hiding the navigation controller. 21

Changing the look of the navigation bar. 22

Xamarin.Forms. 22

Xamarin.Forms is best for. 22

Xamarin.iOS & Xamarin.Android are best for. 22

Using Lists in iOS and Android

iOS Lists

· Add a new Table View Controller to your storyboard. From Visual Studio in your toolbox:

· Next drag the app start icon and associate it to the table view controller(This step is not necessary if you do not want the table view controller to be your first view which starts when your app starts.)

· Next create a new class to the table view by pressing on the dark gray area at the bottom of the view and in the Properties window under Identity there is an attribute named Class. Type in there a new class name which will then be automatically created and will appear in your Visual Studio project.

Using the Table View controller

· Use the Reuse Identifier to minimize the impact on loaded cell, in other words the amount of resources your application will need from the device, things like memory and CPU.

· To do this give a name to the first cell in the Table View Controller in the Identifier attribute in the Properties window.

Populate the table

· You can initialize data in the constructor function which is automatically created:

partial class BooksTableViewController : UITableViewController


List<Book> bookList;

public BooksTableViewController(IntPtr handle) : base(handle)


this.bookList = new List<Book>();

this.bookList.Add(new Book() { Author = “Author 1”, Name = “Book 1”, Editor = “Editor 1”, Year = 2001 });

this.bookList.Add(new Book() { Author = “Author 1”, Name = “Book 2”, Editor = “Editor 1”, Year = 2001 });

this.bookList.Add(new Book() { Author = “Author 1”, Name = “Book 3”, Editor = “Editor 1”, Year = 2001 });



· This is just one way to do it:

1. Override the NumberOfSections() function and set how many sections you need.


2. Override the RowsInSection() function set how many rows in the table will be, this could be the count number of a generic list.


3. Override the GetCell() function to define the cell data for each for being processed. This was defined in the RowsInSection() function. If you had three items in your data source then this function will be called three times. Using the indexPath.Row will get you the row indes which should “normally” correspond to the data source, assuming that the data source can be accessed by an index value.


public override nint NumberOfSections(UITableView tableView)


//return base.NumberOfSections(tableView);

return 1;


public override nint RowsInSection(UITableView tableView, nint section)


//return base.RowsInSection(tableView, section);

return bookList.Count;


// Returns a reusable cell, change it values and returns it to be added to the table view

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)


var cell = tableView.DequeueReusableCell(“Book”);

var dataInTheCell = this.bookList[indexPath.Row];

cell.TextLabel.Text = dataInTheCell.Name;

return cell;

//return base.GetCell(tableView, indexPath);


Modifying what is shown in a cell

· First create class for the table view cell. Go to your story board, in the table view controller select the cell in the upper most part, then go to the Properties window => Widget tab => Identity category and Class. In this attribute type the name of the class you want at it will be created.

· Next you modify how the cell looks or what it contains. You can simply add in new controls, give them a text that is shown by default and an identifier in the Identity category of the Properties window in the Widget tab.

· The next steps is to go to the previously defined table cell class and assign values to the controls in the table view cell. In this example there are four different labels which are set through an Book object which is passed to the table view cell.

partial class BookTableViewCell : UITableViewCell


private Book bookdata;

public Book BookData




return this.bookdata;




this.bookdata = value;

bookAuthorLabel.Text = bookdata.Author;

bookNameLabel.Text = this.bookdata.Name;

bookPublisherLabel.Text = this.bookdata.Editor;

bookYearLabel.Text = this.bookdata.Year.ToString();



public BookTableViewCell (IntPtr handle) : base (handle)




· The last step is to go into the code of the table view controller class an modify the GetCell() function code to use the custom table view cell class which you created previously. After this you can access any member function or property etc through code.

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)


var cell = tableView.DequeueReusableCell(“Book”) asBookTableViewCell;

var dataInTheCell = this.bookList[indexPath.Row];

cell.BookData = dataInTheCell;

return cell;

//return base.GetCell(tableView, indexPath);


Android Lists

· To create a list “view” simply modify a desired activity to inherit from ListActivity instead of Activity. Notice that the SetContentView() function is not called.

MainLauncher =
Icon =

public class MainActivity : ListActivity


int count = 1;

protected override void OnCreate(Bundle bundle)





Populating the view(activity)

· You can populate the activity data in the OnCreate() function

public class MainActivity : ListActivity


List<Book> bookList;

protected override void OnCreate(Bundle bundle)



// Set our view from the “main” layout resource


// Get our button from the layout resource,

// and attach an event to it

this.bookList = new List<Book>();

this.bookList.Add(new Book() { Author = “Author 1”, Name = “Book 1”, Editor = “Editor 1”, Year = 2001 });

this.bookList.Add(new Book() { Author = “Author 1”, Name = “Book 2”, Editor = “Editor 1”, Year = 2001 });

this.bookList.Add(new Book() { Author = “Author 1”, Name = “Book 3”, Editor = “Editor 1”, Year = 2001 });



· Next to define the data to be used in the list and the cell type you can do the following:

var listAdapter = new ArrayAdapter(this, global::Android.Resource.Layout.SimpleListItem1, this.bookList);

· The first parameter in the code is the context where the adapter is to be associated with. The second is the type of the cell in the list and the third parameter is the actual data.

· Next assign the ListAdapter member of the ListActivity class with the newly created listAdapter object.

this.ListAdapter = listAdapter;

· Notice that this approach will use the ToString() function of the class used in the List collection. So you have to override the ToString() function.

Creating a custom cell with custom style and layout

· First create a new layout: Go to your Solution Explorer, in your project go to the layout folder and press the second mouse button, then select Add => New Item, then select Android Layout from the new window.

· Next you can add layout elements to position your content and actual content elements like text elements. You could for example add LinearLayuout (Horizontal) and into them text elements (form widgets)

· Remember to set IDs to your UI elements to be able to access them and a text if you want to

Modifying the layout of an UI element

· This can be achieved from the Properties window under the Layout category. Just change the values in that category to modify how it is laid out .

Creating a custom adapter

· Simply create a new class in your android project

· In the newly created book adapter you have to do the following things:

o Inherit the class from the Android.Widget.BaseAdapter class


o Override the following function:

§ GetView()

§ GetItem()

§ GetItemId()

o The most important function is the GetView(), here you will retrieve the view that is holding your UI elements so that you can access them and modify their values.

public class BooksAdapter : BaseAdapter


public List<Book> books;

Activity activity;

public BooksAdapter(Activity activity)


this.activity = activity;


public override int Count




return this.books.Count;



public override View GetView(int position, View convertView, ViewGroup parent)


var view = convertView ?? activity.LayoutInflater.Inflate(BookStoreAndroid.Resource.Layout.BookCell, parent, false);

var nameLabel = view.FindViewById<TextView>(BookStoreAndroid.Resource.Id.nameTextView);

var authorLabel = view.FindViewById<TextView>(BookStoreAndroid.Resource.Id.authorTextView);

var publisherLabel = view.FindViewById<TextView>(BookStoreAndroid.Resource.Id.publisherTextView);

var yearLabel = view.FindViewById<TextView>(BookStoreAndroid.Resource.Id.yearTextView);

var data = this.books[position];

nameLabel.Text = data.Name;

authorLabel.Text = data.Author;

publisherLabel.Text = data.Editor;

yearLabel.Text = data.Year.ToString();

return view;


public override Java.Lang.Object GetItem(int position)


return null;


public override long GetItemId(int position)


return this.books[position].ID;



Navigation between pages in iOS and Android – Part 2

Navigation between pages in Android

Navigation between pages in iOS

· Generally, to navigate between pages you add new view controller in your storyboard and drag and drop from a UI Element to the desired view control to create a segue.

· After this you select the newly created segue and give it an identifier. You do this by selecting the desired segue and in the Properties window under the Widget tab in the Segue category type in the Identifier attribute

· To transition from using the right segue in the code you can do the following:



public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)


if (segue.Identifier == “MySegue”)


var navigationController = segue.DestinationViewController as MyViewController;

if (navigationController != null)


var rowPath = TableView.IndexPathForSelectedRow;

// Do something with the controller




Navigate to the previous view controller

· Use this simple code

NavigationController.PopViewController (true);

Retrieve Images from iOS phone

· Lets say that you have a button that you want to attach the TouchUpInside event. When this happens you want to allow the user to select an image from the phone.

void ImageButton_TouchUpInside(object sender, EventArgs e)


// Create a new image picker controller

imagePickerController = new UIImagePickerController();

imagePickerController.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;

imagePickerController.MediaTypes = UIImagePickerController.AvailableMediaTypes(UIImagePickerControllerSourceType.PhotoLibrary);

// Attach yourself to the events which are needed

imagePickerController.Canceled += ImagePickerController_Canceled;

imagePickerController.FinishedPickingMedia += ImagePickerController_FinishedPickingMedia;

// Display the image picker as a modal view

NavigationController.PresentModalViewController(imagePickerController, true);


void ImagePickerController_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)


photoImageView.Image = e.Info[UIImagePickerController.OriginalImage] as UIImage;

// This returns the user to the previous view controller



void ImagePickerController_Canceled(object sender, EventArgs e)


// This returns the user to the previous view controller



Sharing Code between Android, iOS or any other platform

· This is rather simple, “normal” C# and Visual Studio stuff. the simplest approach is to create a class library, then create code in it which you want to use in your destination project. Once you are done with your shared class library simply Reference it in Visual Studio and make sure that you are using the right namespaces to access the classes or other code functionality defined in the shared projects.

· Notice: It has to be a portable class library. Otherwise you will not be able to use 3rd party libraries with your application.

· Next you choose your target platform which you want to use with your shared code.

· You could also use some predefined Xamarin project templates with this if you want.

iOS User Interface for different iOS devices using Constraints

· Scaling your application UI to work on different resolutions on different iOS devices

· A quick way to add constraints is to add it from the main.storyboard upper right corner after you have selected an UI control/element

· To specify manually constraints click once then another time on a UI element so that it looks something like this. Pay attention to the new options to specify constraints for the UI element, these are the T shaped icons near the edges of the element.

· To add a new constraint drag a T shaped icon to another element or the edge of the screen telling how the element should behave, how much space for example to leave between this element and another or the screen edge.

Tips and Tricks


Hiding the navigation controller

· In your view controller in the ViewDidAppear or ViewWillDisappear function call the SetNavigationBarHidden()

o Please notice the parameters to the function

public override void ViewDidAppear(bool animated)



this.NavigationController.SetNavigationBarHidden(true, false);


Changing the look of the navigation bar

· You can do this in the AppDelegace.cs file/class, in the overridden function FinnishedLaunching

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)


// Override point for customization after application launch.

// If not required for your application you can safely delete this method

var navigateionVarAppearance = UINavigationBar.Appearance;

navigateionVarAppearance.TintColor = new UIColor(255.0f, 255.0f, 255.0f, 1.0f);

navigateionVarAppearance.BarTintColor = new UIColor(255.0f, 255.0f, 255.0f, 1.0f);

navigateionVarAppearance.SetTitleTextAttributes(new UITextAttributes()


TextColor = new UIColor(255.0f, 255.0f, 255.0f, 1.0f),

TextShadowColor = UIColor.Clear


return true;



From the website:

Xamarin.Forms is best for

· Apps that require little platform-specific functionality

· Apps where code sharing is more important than custom UI

· Developers comfortable with XAML

Xamarin.iOS & Xamarin.Android are best for

· Apps with interactions that require native behavior

· Apps that use many platform-specific APIs

· Apps where custom UI is more important than code sharing

SharePoint Search and changing the Created and LastModified fields

If you want to change the LastModified and Created fields of a list item or a document you can use this code to do that:

using (SPSite site = new SPSite("site + web url"))
using (SPWeb web = site.OpenWeb())
web.AllowUnsafeUpdates = true;
SPListItem listItem = web.GetListItem(this.tbItemURL.Text);

if (this.cCreatedDate.SelectedDate != new DateTime())
listItem[SPBuiltInFieldId.Created] = this.cCreatedDate.SelectedDate;
listItem[SPBuiltInFieldId.Created] = listItem[SPBuiltInFieldId.Created];

if (this.cModifiedDate.SelectedDate != new DateTime())
listItem[SPBuiltInFieldId.Modified] = this.cModifiedDate.SelectedDate;
listItem[SPBuiltInFieldId.Modified] = listItem[SPBuiltInFieldId.Modified];

if (listItem.ParentList.EnableMinorVersions)
if (listItem.ModerationInformation != null)

web.AllowUnsafeUpdates = false;

The reason why the above is done is because if you simply change the date values and use the UpdateOverwireVersion() it will not be enough. The search will not be able to index right values EVEN if you check the UI or use PowerShell or SharePoint Manager etc. All of those show that the value is OK but actually is not. The manually inserted dates will not show up in search unless the item or document is published and approved. So no matter that SP says remember this:).

another thing is that this code above does not allow to set a LastModified IF you have publishing/versioning enabled. After calling the UpdateOverwriteVersion function the item or document will be checked out and remains in that state until something is done. Once you do that you are back to where you started the modified date is changed automatically by SP.

Do not have a solution yet. Maybe you have?

Xamarin Development – Notes, tips and info – Part 1: How to develop your first iOS and Android Apps


This is series of posts and my notes and info which are based on a Udemy course. I’ve taken what I consider “good to know” and wrote them down. This may come handy to someone else.

These are my notes on Xamarin Development.

Presently this post is mostly based on this Udemy Course:


What is Xamarin. 2

Xamarin Platform.. 2

Xamarin Test Cloud. 2

Xamarin Insight. 2

Debugging Android applications. 3

Xamarin Platform.. 4

C# as the language for your mobile app development. 4

Share your C# code on any platform.. 4

Device provisioning/installation of your app. 4

First iOS Application. 4

Basic iOS controls: 5

Properties window.. 5

Naming UI Controls. 5

Events. 6

UI Elements. 6

Different Keyboard types. 6

Changing background colors. 7

Tips and Tricks. 7

First Android Application. 7

Basic Androi controls. 7

Naming UI Controls. 7

Accessing UI elements in the code. 8

UI Elements. 9

Different Keyboard types. 9

Navigation between pages in iOS and Android. 9

Navigation between pages in Android. 9

Adding a new layout and activity. 10

Using Intents to navigate. 10

Passing values between activities (pages/layouts). 10

Navigation between pages in iOS. 11

Adding a new navigation controller. 11

Passing values between views/segues (pages/layouts). 15

What is Xamarin

Xamarin Platform

Building applications on mobile devices

Xamarin apps share code across all platforms

Target i0S, Android, Windows and Mac with a single, shared C# codebase. Use the same language, APIs and data structures on every platform.

C# is the best language for mobile app development

Anything you can do on Objective-C, Swift or Java, you can do in C#

Native UI, native API access, native performance

Xamarin apps are built with standard, native user interface controls. Apps not only look the way the end user expects, they behave that way too. This can’t be achieved with other solutions. They look and behave as they would on that native environment (Android. iOS, Windows Phone)

Xamarin Test Cloud

Test your applications on different devices

Test your app on over 1,000 devices

Run your app on a huge collection of real devices from around the world. Select devices based on form factor, manufacturer, operating system, or even popularity in your target market.

Automate using Ruby or C#

With a framework called Calabash you can automate and test any iOS or Android app, native or hybrid, from the UI level down for perfect integration and continuous improvements.

Analyze app performance

See full-frame screenshots and video playback for every step of every test, then receive performance data and compare reports against previous runs to find regressions and bottlenecks.

Xamarin Insight

Monitoring your applications

Precise, detailed error reporting

Xamarin Insights helps developers prioritize issues by showing exactly which users each crash is impacting, and what sequence of actions preceded the crash.

Understand how users are interacting with your app

Developers can see who’s online, which devices they’re using, what issues they’ve experienced, where they’re from, and which activities they’ve used in sessions.

Integrate your data

Connect your app to a variety of external services and be notified when a new issue is opened, an issue is occurring repeated times in a small amount of time, or an issue’s status changes.

· Github

· Visual Studio Online

· …

Debugging Android applications

· You can use normal Android Virtual Devices with the SDK

· for a FASTER experience use the Xamarin Android Player:

o You also avoid using Hyper-V on Windows

Image: 1 Xamarin Android Player

o To Use the Android Player just go to your Visual Studio toolbar and select a device in this case the device installed in Image: 1 Xamarin Android Player

Image: 2 Example how to use the Android Player

Xamarin Platform

C# as the language for your mobile app development

· Anything you can do in Objective-C, Swift, Java, you can do in C#

· Things which are better than competition(at the present)

o Stronger Types

§ Generics and Compound types are used safely

· No need for casting of comments

· Good tools that understand what you are doing

o Language-level async, made easy in C#

o First-class functions, lambdas are easy to use

Share your C# code on any platform

· Possible to share on average about 75 % of your code across platforms

o Most of it at app logic, common functionality

o Could also be at UI level

§ Xamarin.Forms could be used in theory to achieve 100 % shareable code

Device provisioning/installation of your app

· iOS – Two options:

o Enroll to the Apple Developer Program:


o Or with Xcode 7 use free provisioning:


First iOS Application

· Start with a single view app

· Main.Storyboard we define the UI

· ViewController.cs the main logic goes here

· iOS UI design on implementation requires more work to make it work and look good across different iOS devices

· By default the main.storyboard is linked to the ViewController.cs.

Basic iOS controls:

· Label

· TextField (for inputing data)

· Button

Properties window

· Widget tab

o You can alter button properties such as:

§ Color and how it looks

§ Title

§ Font

§ ID

§ etc

· Layout tab

o How the control is laid out on the UI

· Events tab

o How the user interacts with the control through touch

Naming UI Controls

· This can be achieved through the properties window.

o Select the desired UI control from the main.storyboard

o Go to the Name text field under the Identity category

o Notice: This is a physical name, meaning that you use this in the code to access the control


· When users click/touch on buttons an event is launched.

· You can attach to a click event through the UI or through code by normal C# event handling in the ViewDidLoad() function in the ViewController.cs file

public override void ViewDidLoad()



// Perform any additional setup after loading the view, typically from a nib.

this.convertButton.TouchUpInside += ConvertButton_TouchUpInside;


UI Elements

Different Keyboard types

· You can select different types of keyboard by selecting a text field and under Keyboard Type select the desired keyboard output

Changing background colors

To change the background color of a control simply change the BackgroundColor property. Example>

this.colorView.BackgroundColor = UIColor.FromRGB(redValue, greenValue, blueValue);

Tips and Tricks

Using this.View.EndEditing(true); will end/hide the editing keyboard.

First Android Application

· For Visual Studio development you need Hyper-V to emulate an android device

Basic Androi controls

· Under Text fields category you will find normal input controls

· Under form widgets you will find basic button and plain text controls

Naming UI Controls

· This is found in the properties window under the Main category and in there ID

Accessing UI elements in the code

· In an Android application you have to find the controls manually and assign them to a variable of the same type.

· You use the FindViewById<>() function call. You pass the class type to the template(generic) parameter, then pass the control ID by accessing your project namespace then Resources class, then Id class and finally the control ID which you define in your layout.


using Android.Runtime;

using Android.Views;

using Android.Widget;

using Android.OS;

namespace BMICalculationAndroid


MainLauncher =
Icon =

public class MainActivity : Activity


Button calculateBMI;

TextView resultTextView;

EditText heightEditText, weightEditText;

protected override void OnCreate(Bundle bundle)



// Set our view from the “main” layout resource


this.calculateBMI = FindViewById<Button>(BMICalculationAndroid.Resource.Id.calculateBMI);

this.resultTextView = FindViewById<TextView>(BMICalculationAndroid.Resource.Id.resultTextView);

this.heightEditText = FindViewById<EditText>(BMICalculationAndroid.Resource.Id.heightEditText);

this.weightEditText = FindViewById<EditText>(BMICalculationAndroid.Resource.Id.weightEditText);




UI Elements

Different Keyboard types

· To change to keyboard type when typing on a mobile device you do it similarly as on the iOS side

o In the properties window go to Main – Input Format => inputType and change the keyboard type

Navigation between pages in iOS and Android

Navigation between pages in Android

Adding a new layout and activity

· This allows you to create another UI view/page that is to be displayed in your application

To do this in Visual Studio go to the Resources/Layout folder and add a new item. From the Add New Item window select the Android Layout option.

Then create new activity code class which will operate on the newly created layout.

You can create this for example in your project root by adding again a new item there.

Then to create the link and set the content between the layout and the activity call the SetContentView() function.

protected override void OnCreate(Bundle savedInstanceState)



// Create your application here



Using Intents to navigate

· You navigate to another layout(page) by using the Intent class

· You can use the constructor that takes the present content and the desired activity class associated with the layout

· Then you start the activity and move to the other layout(page) by calling the StartActivity() function

var intent = new Intent(this, typeof(BMIDetailsActivity));


Passing values between activities (pages/layouts)

· In the activity from where you want a value to be passed to another activity you use the Intent class member function named PutExtra() to store the value. It is a key value pair which can be retrieved in another activity later:

var intent = new Intent(this, typeof(BMIDetailsActivity));



· And to restore the value you access it through the activity Intent class member by accessing from it the Extras member. From there use the GetFloat() function.

var bmi = Intent.Extras.GetFloat(“bmi_value”);

Navigation between pages in iOS

Adding a new navigation controller

· Drag and drop a new navigation controller

· If you already have a root view controller, then remove the one which was created by the previous operation (Press the mouse second button on the view controller and press delete)

· Next move is to indicate to the application what is the starting view (Apple story board naming is: sourceless seque). You are using a sourceless seque indicator:

o Drag and drop it on the navigation controller

Image: 3 it should look something like this

· Next create a relationship between the navigation controller and the main view by pressing CTRL + left mouse button on the navigation controller and dragging the mouse over the main view. Then you select the Root option from the new popup. This will mark the main view as the root view.

· Next drag a new view on the storyboard and give it a name. Once you do this the developer tool will warn you that there is no code behind class associated with the view(In Xamarin Studio the UI will offers you to create one). In Visual Studio the easiest way to do it is to press the dark grey area on the view without a controller and writing a new class name in the Class property in the properties window. This will create a new view controller associated to the view selected.

· To navigate from your Main(root) view just select a control that can be pressed like a button, press CTRL + left mouse button on it, then drag to the desired view and let go of the mouse button. In the new popup select Show. This is it. A connection and transition has been made between the views.

Passing values between views/segues (pages/layouts)

· To pass a value between views/segues you can override the function PrepareForSegue() which is launched when the segue is about to be used, that is when the user presses a button or some other UI element which invokes the action of moving from one view/page/layout to another one. What was done earlier.

· In the function you then access the parameter segue and ask for the DestinationViewController class member which you have to cast to the target view controller class. After that it is simply of matter of accessing the defined properties in the view controller class. Same as accessing any public member of a class.

public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)


base.PrepareForSegue(segue, sender);

var detailViewController = segue.DestinationViewController asBMIDetailsViewController2;

if(detailViewController != null)


detailViewController.bmi = this.bmi;