Wednesday, August 17, 2011

SharePoint Collection Enumeration: Did you know that....

This one time I was trying to programmatically modify an SPListCollection, and I stabled into the error "Collection was modified; Enumeration operation may not execute". This error came about during the code block that looped through the SPListCollection and attempting to update the collection.

After some research, I came to find out that it was because I was using "foreach" enumerator, and enumerators can be used to read items in a collection but can not be used to modify them. This was also true for the SPWebCollection, SPListItemCollection, SPFieldCollection etc. I hope this was helpful to someone. Happy coding.

No comments: