0

I have the following CAML query to iterate over a large SharePoint document library :-

 do
    {
        CamlQuery camlQuery6 = new CamlQuery();
        // The RowLimit tells SharePoint to only scan the next 1000 items
        camlQuery6.ViewXml = @"<View><ViewFields><FieldRef Name='Title'/></ViewFields><RowLimit>5000</RowLimit></View>";

        // this tells SharePoint where to start, the first time through this loop
        // 'position' will be null, so it will start at the beginning of the List
        camlQuery.ListItemCollectionPosition = position;

        ListItemCollection collListItem6 = context.Web.GetList(context.Web.ServerRelativeUrl + "/Library3").GetItems(camlQuery6);
        context.Load(collListItem6, items => items.Include(
                                                  item => item.Id,
                                                  item => item["FileDirRef"],
                                                  item => item["Title"],
                                                  item => item["DealStage"],
                                                  item => item["DealName"],
                                                  item => item["Fund"]
                                                ));
                                            context.ExecuteQuery();
                                            // this line grabs the position from where SharePoint left off, 
                                            // so we can pass that in as the starting position the next time through the loop
                                            position = collListItem6.ListItemCollectionPosition;
                                            foreach (ListItem listItem in collListItem6) // collect the items we found in this chuck of searched items
                                            {
                                                foundListItems.Add(listItem);
                                            }
  }
    while (position != null);

but on the context.ExecuteQuery(); i will get this error Value does not fall within the expected range

1 Answer 1

0

Try moving the CamlQuery definition out of the loop so you don't have to keep redefining it and then changing the way you are setting the position, dynamically using the returned value instead of using a variable:

CamlQuery camlQuery6 = new CamlQuery();
// The RowLimit tells SharePoint to only scan the next 1000 items
camlQuery6.ViewXml = @"<View><ViewFields><FieldRef Name='Title'/></ViewFields><RowLimit>5000</RowLimit></View>";

do
{
    ListItemCollection collListItem6 = context.Web.GetList(context.Web.ServerRelativeUrl + "/Library3").GetItems(camlQuery6);
    context.Load(collListItem6, items => items.Include(
          item => item.Id,
          item => item["FileDirRef"],
          item => item["Title"],
          item => item["DealStage"],
          item => item["DealName"],
          item => item["Fund"]
        ));
    context.ExecuteQuery();
    // this line grabs the position from where SharePoint left off, 
    // so we can pass that in as the starting position the next time through the loop
    camlQuery6.ListItemCollectionPosition = collListItem6.ListItemCollectionPosition;

    foreach (ListItem listItem in collListItem6) // collect the items we found in this chuck of searched items
    {
        foundListItems.Add(listItem);
    }
}
while (camlQuery6.ListItemCollectionPosition != null);

Obviously, if this works, this could have ramifications for your previous question.

4
  • your code will return the same error Value does not fall within the expected range
    – John John
    Mar 13, 2020 at 19:51
  • 1
    You have a contradiction, Your CAML specifies to only return one field 'Title', but your Include specifies 6. Try removing your Include statement , context.Load(collListItem6);
    – willman
    Mar 13, 2020 at 19:53
  • i need to include the fileds when loading the context.. i am not sure how i need to write the CAML to just return 5000 items
    – John John
    Mar 13, 2020 at 19:59
  • 1
    If you want to include all those fields then remove <ViewFields><FieldRef Name='Title'/></ViewFields> from your CAML. ViewFields specifies to Only include those fields in the result set.
    – willman
    Mar 13, 2020 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.