Solved

SharePoint ListItemCollection deleting listems

Posted on 2013-05-16
3
732 Views
Last Modified: 2013-05-28
I need to delete listitems within a listitemcollection based on the value of a column.  It fails as I am deleting items within the collection so the enumeration throws exception on next iteration after a delete.  The code follows:

public static void DeletListItemsByParentID(string siteUrl, string listName, string ListIDColumnName, double ItemID)
        {
            try
            {
                using (ClientContext ctx = new ClientContext(siteUrl))
                {
                    ListItemCollection lic = SharePointHelper.GetListItems(ctx, listName);
                    foreach (ListItem l in lic)
                    {
                        object i = l[ListIDColumnName];
                        if ((double)i == ItemID)
                        {
                            l.DeleteObject();
                        }

                    }
                    ctx.ExecuteQuery();
                }

            }
            catch (SystemException ex)
            {
                throw new SystemException("Exception thrown in SharePointHelper.DeletListItemsByParentID", ex);
            }
        }

Open in new window

0
Comment
Question by:Howard Bash
3 Comments
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 39172293
Hi,
you will have to push the list item to another client object and then delete the items:
http://www.jopache.com/blog/batch-delete-sharepoint-list-items-with-javascript
HTH
Rainer
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 39172471
Do you have a C# solution?  The offered solution in javascript.
0
 
LVL 31

Accepted Solution

by:
Jamie McAllister MVP earned 500 total points
ID: 39172528
As you noticed you can't do the delete in a foreach. So, store the unique Guid of the items in e.g. an Arraylist then have a second loop that does the actual deletion after your foreach has finished iterating;

var deletionList = new ArrayList(); 

foreach (ListItem l in lic)
                    {
                        object i = l[ListIDColumnName];
                        if ((double)i == ItemID)
                        {
			    deletionList.Add(l.UniqueId);
                        }

                    }
                                

foreach (Guid g in deleteList)
            {
                try
                {
                    SPListItem deleteItem = myList.GetItemByUniqueId(g);
                    deleteItem.Delete();
                }
                catch (Exception ex)
                {
                    // Handle errors here
                }
            }

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now