0

I need to create custom content type from a given guid for a SharePoint site using Client Object Model. However, in the constructor of

ContentTypeId ctid = new ContentTypeId()

there is no facility to provide value for GUID.

If one notices server side object model to perform the same operation as per http://msdn.microsoft.com/en-us/library/ff798370.aspx then there is an option to provide GUID in the constructor as

SPContentTypeId myContentTypeId = new SPContentTypeId("0x010100FA0963FA69A646AA916D2E41284FC9D9");

Why there is such a difference between Client Object Model and Server Object Model? Is there any possible work around?

2 Answers 2

2

It's not possible to set the content type id in CSOM. Not sure why the difference. The closest you can get is setting the ParentContentType value, but that leaves the final two digits to be randomly generated. This is how to create a content type in CSOM:

var clientContext = new ClientContext( siteUrl );
// For SharePoint Online
var credentials = new SharePointOnlineCredentials(login, secureStringPassword);
clientContext.Credentials = credentials;

var contentTypes = clientContext.Web.ContentTypes;
var contentTypeCreationInformation = new ContentTypeCreationInformation
                                     {
                                         Name = "Content Type Name",
                                         Description = "Description",
                                         Group = "Group",
                                         ParentContentType = ContentType // Must be content type object
                                     };
var contentType = contentTypes.Add( contentTypeCreationInformation );
clientContext.Load( contentType );
clientContext.ExecuteQuery();
clientContext.Dispose();
2
  • This code does not give the option to provide the Content type ID either though Jun 16, 2014 at 13:46
  • 1
    CSOM doesn't provide a method to set the ID on creation. The closest is setting the ParentContentType parameter, but that still leaves the last two digits to random.
    – wjervis
    Jun 16, 2014 at 13:48
2

It's definitely possible to create content types and specifying a content type id:

Web rootWeb = clientContext.Site.RootWeb;
rootWeb.ContentTypes.Add(new ContentTypeCreationInformation {
    Id = "0x0100BDD5E43587AF469CA722FD068065DF5D",
    Name = "My Content Type"
});
clientContext.ExecuteQuery();

Make sure your using the SP2013 Client Components SDK with at least version 15.0.4569.1000 (current is 16.0.3104.1200) (Source).

Latest SDK download: Download

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.