Solved

SharePoint ListItemCollection deleting listems

Posted on 2013-05-16
3
703 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:hbash
3 Comments
 
LVL 44

Expert Comment

by:Rainer Jeschor
Comment Utility
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:hbash
Comment Utility
Do you have a C# solution?  The offered solution in javascript.
0
 
LVL 31

Accepted Solution

by:
Jamie McAllister MVP earned 500 total points
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

We had a requirement to extract data from a SharePoint 2010 Customer List into a CSV file and then place the CSV file into a directory on the network so that the file could be consumed by an AS400 system. I will share in Part 1 how to Extract the Da…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

10 Experts available now in Live!

Get 1:1 Help Now