0

Ques. I have this method wit return type as 'List Item Collection'and my list in SharePoint contains more than 5000 items. In order to overcome the threshold, I need to fetch items in batches by putting row limit but I do not know what to put in the place of ?? so that the return type is also 'ListItemCollection'(SharePoint Client Context). Please help

private static ListItemCollection GetItemsFromSharePointSiteList(string strListName, string strCamlQuery, ClientContext clientContext)
{
    try
    {               
        ListItemCollectionPosition itemPosition = null;

        List listIPPMilestonesLE = clientContext.Web.Lists.GetByTitle(strListName);
        CamlQuery query = new CamlQuery();
        query.ViewXml = strCamlQuery;
        query.DatesInUtc = false;

        //  ListItemCollection itemColl = new ListItemCollection();

        do
        {
            Microsoft.SharePoint.Client.ListItemCollection listItemCollection = listIPPMilestonesLE.GetItems(query);
            clientContext.Load(listItemCollection);
            clientContext.ExecuteQuery();

            foreach (ListItem oListItem in listItemCollection)
            {
                 ??
            }

            itemPosition = listItemCollection.ListItemCollectionPosition;

        } while (itemPosition != null);

        return null;
    }
    catch (Exception exc)
    {
        ErrorLog.Error(exc);
    }

    return null;
}
JumpingJezza
  • 5,498
  • 11
  • 67
  • 106

2 Answers2

0

I did it like below, Hope it will be a workaround and will help you

private static List<ListItem> GetItemsFromSharePointSiteList(string strListName, string strCamlQuery, ClientContext clientContext)
    {
        try
        {
            ListItemCollectionPosition itemPosition = null;

            List listIPPMilestonesLE = clientContext.Web.Lists.GetByTitle(strListName);
            CamlQuery query = new CamlQuery();
            query.ViewXml = strCamlQuery;
            query.DatesInUtc = false;

            var itemColl = new List<ListItem>();

            do
            {
                Microsoft.SharePoint.Client.ListItemCollection listItemCollection = listIPPMilestonesLE.GetItems(query);
                clientContext.Load(listItemCollection);
                clientContext.ExecuteQuery();

                //
                itemColl.AddRange(listItemCollection);

                itemPosition = listItemCollection.ListItemCollectionPosition;

            } while (itemPosition != null);

            return itemColl;
        }
        catch (Exception exc)
        {
            ErrorLog.Error(exc);
        }

        return null;
    }
0

You could set rowlimit in caml query.

For example:

CamlQuery query = new CamlQuery();
                query.ViewXml = "<View><Query><Where><Geq><FieldRef Name='ID'/>" +
                 "<Value Type='Number'>1</Value></Geq></Where></Query><RowLimit>100</RowLimit><ViewFields><FieldRef Name='ID'/><FieldRef Name='Title'/></ViewFields></View>";

Then, Paging with ListItemCollectionPosition property.

Check this sample.

Lee
  • 5,305
  • 1
  • 6
  • 12