How to delete a site-column reference from a content-type in Sharepoint Client model

拜拜、爱过 提交于 2019-12-12 00:02:00

问题


I am trying to delete a site-columns from the sharepoint website directly from my code. These site-columns are currently referenced under some content-types. So when I execute a code

//Delete the site-column
conFields.DeleteObject();
clientContext.ExecuteQuery();
break;

it throws an exception

Site columns which are included in content types cannot be deleted. Remove all references to this site column prior to deleting it.

Can anyone please suggest a way to first remove that reference from the content-type and then delete the site-column.

Here's the code:

//availableCT is my content-type
 FieldCollection fieldColl = availableCT.Fields;
clientContext.Load(fieldColl);
clientContext.ExecuteQuery();
foreach (Field field in fieldColl)
{
  //columnFromList is the column taht is to be deleted
  if (field.InternalName.Equals(columnFromList))
  {
    field.DeleteObject();
    clientContext.executeQuery();
  }
}

Whenever I'm running this code, it throws me an exception:

Additional information: Site columns which are included in content types or on lists cannot be deleted. Please remove all instances of this site column prior to deleting it.

Please suggest me a way to achieve this task programmatically. FYI, when I try to delete it from my Sharepoint website, it gets deleted without any error.


回答1:


Since the site column is referenced in content type the specified error occurs.

The following examples (implemented as Extension methods) demonstrate how to delete site columns when it is referenced in content type(s):

public static class FieldExtensions
{
    /// <summary>
    /// Remove column and reference from specific content types
    /// </summary>
    /// <param name="field"></param>
    /// <param name="contentTypeId"></param>
    public static void DeleteObject(this Field field,string contentTypeId)
    {
        var ctx = field.Context as ClientContext;
        if (!field.IsPropertyAvailable("Id"))
        {
            ctx.Load(field, f => f.Id);
            ctx.ExecuteQuery();
        }
        //Firstly, remove site column from content type
        var contentType = ctx.Site.RootWeb.ContentTypes.GetById(contentTypeId);
        var fieldLinks = contentType.FieldLinks;
        var fieldLinkToRemove = fieldLinks.GetById(field.Id);
        fieldLinkToRemove.DeleteObject();
        contentType.Update(true); //push changes
        //Then remove column
        field.DeleteObject();
    }


    /// <summary>
    /// Remove column and references from all content types
    /// </summary>
    /// <param name="field"></param>
    /// <param name="includeContentTypes"></param>
    public static void DeleteObject(this Field field, bool includeContentTypes)
    {
        var ctx = field.Context as ClientContext;
        if (!field.IsPropertyAvailable("Id"))
        {
            ctx.Load(field, f => f.Id);
            ctx.ExecuteQuery();
        }
        //Firstly, remove site column link from all content types
        ctx.Load(ctx.Site.RootWeb.AvailableContentTypes, cts => cts.Include(ct => ct.FieldLinks));
        ctx.ExecuteQuery();
        foreach (var ct in ctx.Site.RootWeb.AvailableContentTypes)
        {
            var containsField = ct.FieldLinks.Any(fl => fl.Id == field.Id);
            if (containsField)
            {
                var fieldLinkToRemove = ct.FieldLinks.GetById(field.Id);
                fieldLinkToRemove.DeleteObject();
                ct.Update(true); //push changes         
            }
        }
        //Then remove site column
        field.DeleteObject();
    }
}

Usage

Delete site column and references from all content types:

using (var ctx = ClientContext(webUri))
{        
    var siteFields = ctx.Site.RootWeb.Fields;
    var fieldToDel = siteFields.GetByInternalNameOrTitle(fieldName);
    fieldToDel.DeleteObject(true);
    ctx.ExecuteQuery();
}

Delete site column and reference from content type:

using (var ctx = ClientContext(webUri))
{        
   //find content type
   var result = ctx.LoadQuery(ctx.Site.RootWeb.AvailableContentTypes.Where(ct => ct.Name == "Order Document"));
   ctx.ExecuteQuery();

   if (result.Any())
   {
       var ctId = result.First().Id.StringValue;
       var siteFields = ctx.Site.RootWeb.Fields;
       var fieldToDel = siteFields.GetByInternalNameOrTitle(fieldName);
       fieldToDel.DeleteObject(ctId);
       ctx.ExecuteQuery();
   }
}


来源:https://stackoverflow.com/questions/28319613/how-to-delete-a-site-column-reference-from-a-content-type-in-sharepoint-client-m

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!