1

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.

  • Is last name your custom column? – Aanchal Jun 20 '14 at 10:19
  • No, "Last Name" is one of the default columns that get added to a Contact List. – Carl Heinrich Hancke Jun 20 '14 at 10:53
1

You cannot delete a field with CanBeDeleted property as false as it indicates whether the field can be deleted or not.

  • OK, but why would I be able to delete that same column through Visual Studio? In another app I've added a Contact List feature to the app & then deleted this same column without issue. – Carl Heinrich Hancke Jun 20 '14 at 11:09
  • You are not deleting the column, its just you are not using that column in your custom list. But in default Contact list , it comes by default. – Aanchal Jun 20 '14 at 11:13
  • Ah, that makes more sense. My perception of what was happening in the background was flawed then. Thank you, I'll work on hiding these columns away then, rather than deleting them. – Carl Heinrich Hancke Jun 20 '14 at 11:15
  • Lists and libraries contain required columns that can’t be deleted, such as the Title or Name (last name in your case). If the column can’t be deleted, the Delete button is not available. If you can’t delete a column, but you don’t want the column to appear in a view, you can remove it from the view. – Aanchal Jun 20 '14 at 11:17
1

maybe you try it with setting the field not to be readonly anymore

In C#:

f.ReadOnlyField = false;
f.Update(true);
s.Lists[ListName].Update();
f.Delete();
  • Just for the sake of completeness, I tested your solution. It still comes up with the same exception. So the only ways seems to hide these columns from view, as Aanchal suggested. – Carl Heinrich Hancke Jun 20 '14 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.