我正在尝试更改文档库中所有“根文件夹”的内容类型。我什至不确定这是否可行。当我运行下面的代码时,我得到的消息是ListItem.ContentType被写保护...
我的问题是,是否可以更改内容类型? 如果是,我该如何使用CSOM?
谢谢
ContentType ct = list.ContentTypes.GetById("0x0120D520008AE499F0AEB1C647B9D6F0C9D3B7F9F100B56E2AEF9C715540BE5E87A04F54476E");
context.ExecuteQuery();
foreach (ListItem item in items)
{
context.Load(item, i => i.DisplayName);
context.Load(item, i => i.ContentType);
context.Load(ct, i => i.Id);
context.ExecuteQuery();
if (item.ContentType.Name == "Folder")
{
Console.WriteLine("Name: " + item.DisplayName + " ContentType:" + item.ContentType.Name);
if (item.ContentType.Sealed = true)
{
item.ContentType.Sealed = false;
item.Update();
context.ExecuteQuery();
}
item.ContentType = ct.Id;
item.Update();
context.ExecuteQuery();
}
}
答案 0 :(得分:0)
像这样更新项目内容类型,设置ContentTypeId字段值:
List list = ctx.Web.Lists.GetByTitle("doc2");
ContentType ct = list.ContentTypes.GetById("0x0120001D61DFC51D574148B41D5DEB19779D19000C2B25DED7B1C34BB491C5BE59765450");
ctx.Load(ct);
ctx.ExecuteQuery();
CamlQuery caml = new CamlQuery();
ListItemCollection items = list.GetItems(caml);
ctx.Load(items);
ctx.ExecuteQuery();
foreach (ListItem item in items)
{
ctx.Load(item, i => i.DisplayName);
ctx.Load(item, i => i.ContentType);
ctx.Load(ct, i => i.Id);
ctx.ExecuteQuery();
if (item.ContentType.Name == "Folder")
{
item["ContentTypeId"] = ct.Id.ToString();
item.Update();
ctx.ExecuteQuery();
}
}