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();
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…