Question

I have created one application for deleting Duplicate SPListItem. The list has thousands of duplicate records. While deleting the item (say, approx. after 1000 records deletion). The Application completing the deletion process and showing the successfully completed message. I am appending My code snippet for deleting the items .

spFormIDCollection = spHistoryList.GetItems(query);
                            if (spFormIDCollection != null && spFormIDCollection.Count > 0)
                                for (int i = 0; i < spFormIDCollection.Count - 1; i++)
                                {
                                    SPListItem listItem = spFormIDCollection[i];
                                    if (listItem != null)
                                        if (Convert.ToString(listItem[Common.NotificationDate]).Equals(Convert.ToString(spFormIDCollection[i + 1][Common.NotificationDate])))
                                            listItem.Delete();

                                }

I don't know what went wrong with my code. Can you please explain, if there is any incorrect code. Thanks in advance..

Était-ce utile?

La solution

You should change your for-loop to

for (int i = spFormIDCollection.Count - 1; i > -1; i--)

When deleting items the collection.Count decreases and your loop will finish to early.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top