I have a team site were I have created a document library with documents in it, I am trying to set a column in the document library to be empty.
But the column does not get empty?
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = (SPWeb)properties.Feature.Parent;
SPList list = web.Lists.TryGetList("Document Library");
if (list != null)
{
SPListItemCollection listItemCollection = list.GetItems();
foreach (SPListItem item in listItemCollection)
{
var columnToUpdate ="MyField";
string internalName = item.Fields[columnToUpdate].InternalName;
item[internalName] = "";
item.Update();
}
}
}
I guess you should find another way to empty your field than in foreach loop, because foreach is meant to iterate over a container, making sure each item is visited exactly once, without changing the container, to avoid nasty side effects(i.e. foreach is read-only). Maybe trying to make similar steps with for loop helps, like in this code:
Hope it will help. Also I recommend ask this question on https://sharepoint.stackexchange.com/ if you won't find solution here.