客户端对象模型将内容类型添加到列表

use*_*851 2 sharepoint content-type sharepoint-clientobject

我创建了一个SP.List项"lst".我也:

lst.ContentTypesEnabled = true;
lst.Update();
clientContext.ExecuteQuery();
Run Code Online (Sandbox Code Playgroud)

我搜索了AvailableContentTypes并找到了我要添加到lst的那个.然后我:

SP.ContentTypeCollection lstTypeCollection = lst.ContentTypes;
Run Code Online (Sandbox Code Playgroud)

......现在我被卡住了.
lstTypeCollection.Add()想要一个ContentTypeCreationInformation物体,我在黑暗中徘徊.

你能摆脱光明吗?提前致谢 :-)

Vad*_*hev 5

使用ContentTypeCollection.AddExistingContentType方法将现有内容类型添加到列表中.

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();
Run Code Online (Sandbox Code Playgroud)