Solved

SharePoint ListItemCollection deleting listems

Posted on 2013-05-16
3
802 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connect Gridview column to Textbox in C# 2 55
c# ftp code 3 56
asp.net, radiobuttonlist, c# 3 69
Need a quick overview of Selenium testing 2 36
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

752 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