Link to home
Start Free TrialLog in
Avatar of Howard Bash
Howard BashFlag for United States of America

asked on

SharePoint ListItemCollection deleting listems

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

Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

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
Avatar of Howard Bash

ASKER

Do you have a C# solution?  The offered solution in javascript.
ASKER CERTIFIED SOLUTION
Avatar of Jamie McAllister
Jamie McAllister
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial