标题说明了一切。如何以编程方式从内容类型中删除字段引用?
我到目前为止所做的尝试:
public void RemoveField(ClientContext ctx, Web web, ContentType type, Field field) // doesnt do anything
{
try
{
FieldLinkCollection fields = type.FieldLinks;
FieldLink remove_field = fields.GetById(field.Id);
remove_field.DeleteObject();
ctx.ExecuteQuery();
}
catch (Exception ex)
{
throw ex;
}
}
这没有做任何事情(也不例外)。
我在论坛中找到了另一种方式:
contentType.FieldLinks.Delete(field.Title);
contentType.Update();
但是删除(field.Title)方法似乎不存在于CSOM中。
THX
答案 0 :(得分:3)
由于正在修改内容类型,因此必须显式调用更新内容类型(ContentType.Update method)的方法:
//the remaining code is omitted for clarity..
remove_field.DeleteObject();
ctx.Update(true); //<-- update content type
ctx.ExecuteQuery();
以下示例演示了如何使用CSOM从内容类型中删除网站列
using (var ctx = new ClientContext(webUri))
{
var contentType = ctx.Site.RootWeb.ContentTypes.GetById(ctId);
var fieldLinks = contentType.FieldLinks;
var fieldLinkToRemove = fieldLinks.GetById(fieldId);
fieldLinkToRemove.DeleteObject();
contentType.Update(true); //push changes
ctx.ExecuteQuery();
}
答案 1 :(得分:0)
我的最终工作代码:
public void RemoveField(ClientContext ctx, Web web, ContentType type, Field field) // doesnt do anything
{
try
{
FieldLinkCollection flinks = type.FieldLinks;
FieldLink remove_flink = flinks.GetById(field.Id);
remove_flink.DeleteObject();
type.Update(true);
ctx.ExecuteQuery();
}
catch (Exception ex)
{
throw ex;
}