Reputation: 25
the title says it all. how can i programmatically remove a field reference from a content type?
what i've tried so far:
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;
}
}
this doesnt do anything (also no exception).
i've found another way in a forum:
contentType.FieldLinks.Delete(field.Title);
contentType.Update();
but the Delete(field.Title) method doesnt seem to exist in the CSOM.
thx
Upvotes: 2
Views: 4170
Reputation: 25
my final working code:
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;
}
Upvotes: 0
Reputation: 59328
Since content type is being modified, the method for updating content type (ContentType.Update method) have to be explicitly invoked:
//the remaining code is omitted for clarity..
remove_field.DeleteObject();
ctx.Update(true); //<-- update content type
ctx.ExecuteQuery();
The following example demonstrates how to delete site column from content type using 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();
}
Upvotes: 3