Team site with document library

91 views Asked by At

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();
        }
    }
}
1

There are 1 answers

0
user2771704 On

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:

    SPListItemCollection listItemCollection = list.GetItems();

    for (int i = 0; i < listItemCollection.Count; i++)
    {
        var columnToUpdate = "MyField";
        var item = listItemCollection[i];
        string internalName = item.Fields[columnToUpdate].InternalName;

        item[internalName] = "";
        item.Update();
    }

Hope it will help. Also I recommend ask this question on https://sharepoint.stackexchange.com/ if you won't find solution here.