Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...