Thursday 11 June 2015

Get list data from a SharePoint List when you have more than 5000 rows/records.





Here we are  retrieving the items based on listItemcollectionPosition.

One can use the ListItemCollectionPosition class to implement paging list item retrieval according to the position of items relative to their collection. Use the RowLimit element to specify the number of items to return per page. The following example loops through all the items in a SharePoint list, using the ListItemCollectionPosition property of the CamlQuery class, and the ListItemCollectionPosition property of the ListItemCollection class.



  ClientContext context = new ClientContext"Site URL");
 string username = @"username";
  string passw = @"password";
  SecureString passWord = new SecureString();
  foreach (char c in passw.ToCharArray()) passWord.AppendChar(c);
             {
     context.Credentials = new SharePointOnlineCredentials(username, passWord);

            }
  Web web = context.Web;
 try
         {
         int count = 0;
        List  Listname= context.Web.Lists.GetByTitle("name of the list");

         ListItemCollectionPosition itemPos = null;

          int intCount = 0;
          while (true)
               {
           CamlQuery cQuery = new CamlQuery();
             cQuery.ListItemCollectionPosition = itemPos;
             cQuery.ViewXml = "<View>"
               + " <RowLimit>1000</RowLimit>"
               + " </View>";

        Microsoft.SharePoint.Client.ListItemCollection items = Listname.GetItems(cQuery);
        context.Load(items);
        context.ExecuteQuery();
        itemPos = items.ListItemCollectionPosition;
        for (int iCntr = items.Count - 1; iCntr >= 0; iCntr--)

                    {

                      intCount++;
                      Microsoft.SharePoint.Client.ListItem listItem = items[iCntr];
                       listItem["Status"] = "Active";
             //Item is Retreivedinlistitem and now we are updating it to active
                      listItem.Update();
                     context.ExecuteQuery();

                    }

                    if (itemPos == null)

                    {

                        break;

                    }

                }

            }

            catch (Exception ex)

            {
               System.IO.StreamWriter file = new System.IO.StreamWriter("log Path", true);
                file.WriteLine(DateTime.Now + "    " + ex.Message);
                file.Close();



            }

No comments: