以编程方式从内容类型中删除字段引用 (CSOM)

Remove field reference from content type programmatically (CSOM)

标题说明了一切。我如何以编程方式从内容类型中删除字段引用?

到目前为止我尝试过的:

    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();

但是 CSOM 中似乎不存在 Delete(field.Title) 方法。

感谢

由于正在修改内容类型,因此必须显式调用更新内容类型的方法 (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();
}

我的最终工作代码:

    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;
        }