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.