-->

client object model add content type to list

2019-05-25 10:56发布

问题:

I have created an SP.List item "lst". I also:

lst.ContentTypesEnabled = true;
lst.Update();
clientContext.ExecuteQuery();

I have searched the AvailableContentTypes and found the one I want to add to lst. I then:

SP.ContentTypeCollection lstTypeCollection = lst.ContentTypes;

... now I'm stuck.
the lstTypeCollection.Add() wants a ContentTypeCreationInformation object at that point I'm wandering in the dark.

Can you shed light? Thanks in Advance :-)

回答1:

Use ContentTypeCollection.AddExistingContentType method to add an existing content type to the list.

Example

var list = context.Web.Lists.GetByTitle(listTitle);
list.ContentTypesEnabled = true;

var contentType = context.Site.RootWeb.ContentTypes.GetById("0x0120"); //get Folder content type
list.ContentTypes.AddExistingContentType(contentType);
context.ExecuteQuery();