We cannot use the following code, we may get the following error:
"Collection was modified; enumeration operation may not execute."
using (SPSite site = new SPSite("http://server")) {
using (SPWeb web = siteCollection.OpenWeb()) {
SPList list = web.Lists["MyList"];
foreach (SPListItem item in list.Items) {
item.Delete();
}
}
}
We also cannot use the following code:
for (int i = 0; i < list.Items.Count; i++) {
list.Items.Delete(i);
}
The correct code is as follows:
The correct method for deleting list items is to use a decrementing For loop.
for (int i = list.Items.Count - 1; i >= 0; i--) {
list.Items.Delete(i);
}
For details refer the following link.
Vinay Chavadi's Blog on Application Development in SharePoint 2010/2013/2016 & SharePoint Online
Subscribe to:
Post Comments (Atom)
-
There are 3 SPJobLockType available: 1. SPJobLockType.None -- if you set it none, the instance will run in all the available s...
-
Most of you who are working on Remote Event Receivers must have encountered this issue of "Updated event getting fired multiple times&...
No comments:
Post a Comment