Developing for SharePoint Online, I've created a Contact List programmatically in my app. I now want to delete some of the columns that were added to the list. So I have the following code:
var lastName = list.Fields.GetByInternalNameOrTitle("Last Name");
if (lastName != null) {
lastName.DeleteObject();
}
. . .
clientContext.ExecuteQuery();
But this does not delete the column. In fact, when contained in a try {...} catch {...}
, I get the following exception message:
Operation is not valid due to the current state of the object.
Looking at the various properties on the Field
object, I can see the CanBeDeleted
property is false
, so I can only assume that this is the cause for the above message. But this property is also read-only, so I can't force it to be deleted.
So my question is, how would I go about deleting these columns? When I add a Contact List to my app using Visual Studio, I'm able to delete these columns through the tooling, so I want to do the same in code.
Any help would be greatly appreciated.