Showing posts with label The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.. Show all posts
Showing posts with label The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.. Show all posts

Friday, February 9, 2018

Get All List Items from SharePoint List page wise to handle threshold limit


Below is the common error during development:
The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.
How to handle in client object model?
  • To avoid threshold error, we can use below orderby query.
OrderBy Override='TRUE'>

  • Using Sorting on the returned list
items = items.OrderBy(item => item.FieldValues["FileLeafRef"]).ToList();
  • Implementing the search on the items. Sample below which brings matched document string name on the page
items = items.FindAll(f => f.FieldValues["FileLeafRef"].ToString().ToLower().Contains("".ToLower())).ToList();

  • Complete Method

public static List<ListItem> GetAllListItemsInaList1(ClientContext ctx, string siteurl)

        {

List<ListItem> items = new List<ListItem>();

            ctx.Load(ctx.Web, a => a.Lists);

            ctx.ExecuteQuery();

List list = ctx.Web.Lists.GetByTitle("EngagementDocumentLibrary");

ListItemCollectionPosition position = null;

int rowLimit = 4999;

var camlQuery = new CamlQuery();

            camlQuery.ViewXml = @"<View Scope='RecursiveAll'>

            <Query>

<OrderBy Override='TRUE'><FieldRef Name='ID'/></OrderBy>              

            </Query>

            <ViewFields>

                <FieldRef Name='FileLeafRef'/><FieldRef Name='File_x0020_Type' /><FieldRef Name='FileRef' /><FieldRef Name='Editor' />

            </ViewFields>

            <RowLimit Paged='TRUE'>" + rowLimit + "</RowLimit></View>";

do

            {

ListItemCollection listItems = null;

                camlQuery.ListItemCollectionPosition = position;

                listItems = list.GetItems(camlQuery);

                ctx.Load(listItems);

                ctx.ExecuteQuery();

                position = listItems.ListItemCollectionPosition;

                items.AddRange(listItems.ToList());

            }

while (position != null);

            items = items.OrderBy(item => item.FieldValues["FileLeafRef"]).ToList();

return items;

        }