Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SharePoint ListItemCollection deleting listems

Posted on 2013-05-16
3
Medium Priority
?
993 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 32

Accepted Solution

by:
Jamie McAllister MVP earned 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

916 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