Search code examples
c#sharepointsharepoint-onlinesharepoint-list

Sharepoint online - Not returning items in a specific view in List


I am trying to get a list of items from a specific view. Below is the code

Microsoft.SharePoint.Client.List _lists = context.Web.Lists.GetByTitle("Invoice Register");  
context.Load(_lists);
context.ExecuteQuery();
int listCount = _lists.ItemCount; // i get 49000+ count here
                
View _listsView = _lists.Views.GetByTitle("IT Testing");
context.Load(_listsView);
context.ExecuteQuery();
                

CamlQuery _query = new CamlQuery();
_query.ViewXml = _listsView.ViewQuery;


Microsoft.SharePoint.Client.ListItemCollection items = _lists.GetItems(_query);           
context.Load(items);            
context.ExecuteQuery();
int _viewCount = items.Count; // I get nothing here.
           

The error I get is The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator

I have created indexes already enter image description here

I have set the limit for "IT Testing" to 5000 as seen here. enter image description here

If someone can guide, it would be helpful. I have gone through all the links I could.

Regards


Solution

  • @Ather Siddiqui,

    there is no direct way to get items under a view as the view only has query schema and does not have any items. The caml query may get the same items that are under the view, but it will trigger the list view threshold.

    @user2250152 has provided a good method, it's possible to get over 5000 items through Pagination. If you want to use the view query, you could change the query as below:

    List tList = context.Web.Lists.GetByTitle("My test list");
                CamlQuery camlQuery = new CamlQuery
                {
                    ViewXml = @"<View><Query><Where><Gt><FieldRef Name='ID' /><Value Type='Counter'>20</Value></Gt></Where></Query><OrderBy><FieldRef Name ='FileLeafRef' /></OrderBy><RowLimit>4990</RowLimit></View>"  // your view query
                };
    
                var itemColl = new List<ListItem>();
             
                do
                {
                    ListItemCollection listItemCollection = tList.GetItems(camlQuery);
                    context.Load(listItemCollection);
                    context.ExecuteQuery();    
                    
                    //
                    itemColl.AddRange(listItemCollection);
                    camlQuery.ListItemCollectionPosition = listItemCollection.ListItemCollectionPosition;               
    
                } while (itemColl.Count < 999); //view row limit   
    
                Console.WriteLine(itemColl);