client object model add content type to list

2019-05-25 11:09发布

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条回答
闹够了就滚
2楼-- · 2019-05-25 11:55

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();
查看更多
登录 后发表回答