Saturday, February 26, 2011

SharePoint - Delete and update list items programmatically and Collection modified Exception

In One of my exercise there is a requirement to delete the list items in SharePoint programmatically. For that we need to search the entire list item collection in the list.By Deleting the item in the list we need to break the loop. Because in the SharePoint the collection will be reloaded in the memory, and the list collection is not available for looping. Through this collection modified Exception will raise here. For updating a single item in the SharePoint we have the same problem.

This is the code to Delete the list item in the SharePoint.
// Get the list items in the particular list, Delete the List item. 
static void Main(string[] args)
{
  string siteUrl = string.Empty;
  string listTitle = string.Empty;
  string listItemTitle = string.Empty;
  bool isItemFound = false;
  SPListItemCollection listItemCollection;
  Console.WriteLine("Enter the url of the site");
  try
  {
       siteUrl = Console.ReadLine();
       if (string.IsNullOrEmpty(siteUrl))
       {
          Console.WriteLine("url not be empty or null");
       }
       else
       {
           using (SPSite currentSite = new SPSite(siteUrl))
           {
               using (SPWeb currentWeb = currentSite.OpenWeb())
               {
                  if (currentWeb.Exists)
                  {
                        SPListCollection listCollection = currentWeb.Lists;
                        Console.WriteLine("Enter title of the list");
                        listTitle = Console.ReadLine();
                        if (string.IsNullOrEmpty(listTitle))
                        {
                              Console.WriteLine("list title not be empty");
                        }
                        else
                        {     
                              listItemCollection =currentWeb.Lists[listTitle].Items;                        
                              Console.WriteLine("enter the title of the item to delete");
                              listItemTitle = Console.ReadLine();
                              if (string.IsNullOrEmpty(listItemTitle))
                              {
                                 Console.WriteLine("list item name not empty or null");
                              } 
                              else
                              {
                                 for(int i=0;i< listItemCollection.Count;i++)
                                 {
                                   if (listItemCollection[index].Title.Equals(listItemTitle, StringComparison.InvariantCultureIgnoreCase))
                                    {
                                       listItemCollection[index].Delete();
                                       isItemFound = true;
                                       break;
                                    }
                                 }
                              }
                        }
                  }             
               }
           }
       }
   }
   catch (Exception e)
   {
       Console.WriteLine(e.Message());
   }
   Console.ReadKey();
}
Here the the loop will process the entire list item collection. If the element that to deleted is found then it will be deleted and the loop will break over there. Through this we cannot get any exception in code. If element not found it will return that the element not deleted. For updating the list items also follows the same code. Hope this will help you. .

Share this

0 Comment to "SharePoint - Delete and update list items programmatically and Collection modified Exception"

Post a Comment