Solved

SharePoint ListItemCollection deleting listems

Posted on 2013-05-16
3
745 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

SharePoint Designer 2010 has tools and commands to do everything that can be done with web parts in the browser, and then some – except uploading a web part straight into a page that is edited in SPD. So, can it be done? Scenario For a recent pr…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

776 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