One of the limitations what we have been having within the SharePoint 2013 Client Side Object Model (CSOM), was the lack of capability to create content types with specific identifier. This has been pretty frustrating restriction when we have been helping our customer to transform from the full trust code (FTC) to the cloud app model (CAM).
When you’re using full trust code, this has been commonly addressed either using server side code or by using feature framework xml’s (try to avoid this). When we are however using remote provisioning patterns in Office365, we have not been able to create content types with specific identifiers, which have caused inconsistencies cross site collections. You might have used content type hub to address this in past, but now with SP2013 SP1, this capability has been included to the updated SharePoint 2013 CSOM APIs.
You can download the updated SP2013 SP1 Client Components SDK from following URL. Notice that the version of the Microsoft.SharePoint.Client.dll has been updated and SP1 version has file version set as 15.0.4569.1000.
After you’ve started to use the new versions, we can simply assign the Content type ID in our code as follows.
1: // Open connection to Office365 tenant
2: ClientContext cc = new ClientContext(siteUrl);
3: cc.AuthenticationMode = ClientAuthenticationMode.Default;
4: cc.Credentials = new SharePointOnlineCredentials(userName, pwd);
6: // Load reference to content type collection
7: Web web = cc.Web;
8: ContentTypeCollection contentTypes = web.ContentTypes;
11: // Create a Content Type Information object
12: ContentTypeCreationInformation newCt = new ContentTypeCreationInformation();
13: // Set the name for the content type
14: newCt.Name = "Contoso Document";
15: //Inherit from oob document - 0x0101 and assign
16: newCt.Id = "0x0101009189AB5D3D2647B580F011DA2F356FB2";
17: // Set content type to be avaialble from specific group
18: newCt.Group = "Contoso Content Types";
19: // Create the content type
20: ContentType myContentType = contentTypes.Add(newCt);
Notice that when you assign the Id property directly, you cannot set the ParentContentType property for the ContentTypeCreationInformation object. If you do so, you will get an exception message telling that the combination is not good like in below picture.
Here’s quick video showing how to create content types to MSO using specific identifier from console application. Example is downloadable from the links under reference section.
Here’s quick list of some useful links related on this blog post.