Delen via


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

  • Anonymous
    February 27, 2014
    Hi Vesa, thanks a lot for sharing this!

  • Anonymous
    February 28, 2014
    Awesome!

  • Anonymous
    March 02, 2014
    very nice.

  • Anonymous
    April 21, 2014
    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

  • Anonymous
    April 22, 2014
    The comment has been removed

  • Anonymous
    April 27, 2014
    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?

  • Anonymous
    May 06, 2014
    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.

  • Anonymous
    June 28, 2014
    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!

  • Anonymous
    September 21, 2014
    Hi Vesa, Thanks! This helped us today while creating CT using CSOM on OnPremises.

  • Anonymous
    January 30, 2015
    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.
  • Anonymous
    January 30, 2015
    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.

  • Anonymous
    March 30, 2015
    Nice post.

  • Anonymous
    December 18, 2017
    Thanks :))