FTC to CAM – Create Content Types with specific IDs using CSOM


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);
   5:  
   6: // Load reference to content type collection
   7: Web web = cc.Web;
   8: ContentTypeCollection contentTypes = web.ContentTypes;
   9: cc.Load(contentTypes);
  10: cc.ExecuteQuery();
  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);
  21: cc.ExecuteQuery();

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.

image

Demo of the code

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.

References

Here’s quick list of some useful links related on this blog post.


Comments (12)

  1. Arthur Neufeld says:

    Hi Vesa, thanks a lot for sharing this!

  2. Ali Mazaheri says:

    Awesome!

  3. Mallikarjun Tuppad says:

    very nice.

  4. Kythor says:

    Hi Vesa, this is not working in my project. I have installed the dll update and used the exact same code as you. But the content type is created with a random ID and inherits from "item" content type.

    On MSDN I cannot find info about setting the ID…

    any help is appreciated

  5. Vesa Juvonen says:

    Hi Kyhor,

    please make sure that you're referencing the right versions in the VS solution. This API is only available in the SP1 updated CSOM components.

    MSDN updates are always delayed… they are coming, but getting official content updated always takes a while.

  6. Kythor says:

    Vesa, that's exactly what I did. If I check the references, Microsoft.SharePoint.Client.dll has been updated and has file version 15.0.4569.1000.

    I also installed the update on the sharepoint server to be sure.

    But still no success…

    Are there any other DLL's involved that should have the right file version?

  7. Vesa Juvonen says:

    Hi Kythor,

    that should be enough to do the trick, but can't really know what's now failing with this information. I've though tested this against Office365 and this could be simply a mix up for the server side APIs in your on-premises farm.

  8. Ivan Inyushin says:

    I had the same issue, and the problem was that SP1 was not installed on the server side (when trying against an on-premises farm). On SharePoint Online it worked without any issues. Thanks Vesa!

  9. Prasham says:

    Hi Vesa,

    Thanks! This helped us today while creating CT using CSOM on OnPremises.

  10. Martin says:

    This is great but two remaining limitations when provisioning content types via CSOM are:

    1) No way to set the fieldlink name different to the field name as we can with SSOM

    2) No way to inject the XmlDocuments section, which means limitations with Document Set content types for example. We can't set SharedFields, WelcomePageFields, etc.

  11. Vesa Juvonen says:

    Thanks Martin for the feedback. I would suggest to use the officespdev.uservoice.com for submitting these kind of gaps, so that we them tracked. We do have weekly triage on all submissions and this kind of feedback is highly appreciated.

  12. Muhammad Masood says:

    Nice post.

Skip to main content