user3803807
user3803807

Reputation: 315

SharePoint - Get count of items in a view

I'm trying to return the count of items in a SharePoint view. I have tried using several api calls like below but don't really understand how to target the specific list. I keep getting the error 'Cannot find resource for the request Views.'. I have also tried substituting the view name to the view id as well. Any suggestions for a api request I can use in postman would be appreciated

Sample API request in postman

https://[SHAREPOINT]/sites/TestSJTemp/_api/web/GetFolderByServerRelativeUrl('/sites/TestSJTemp/Export Applications')/Views/getbytitle('MAN eCase 2003')

Web URL of the list I'm trying to get a count from

https://[SHAREPOINT]/sites/TestSJTemp/EXP2/Forms/eCase%20Document%20Type%20View.aspx?newTargetListUrl=%2Fsites%2FTestSJTemp%2FEXP2&viewpath=%2Fsites%2FTestSJTemp%2FEXP2%2FForms%2FeCase%20Document%20Type%20View%2Easpx&viewid=aef46c6f%2D4901%2D4e3e%2D876f%2D6b55549b945d

Image of the sharepoint page and structure. I select the view which is highlighted and in this instance can see the count is 35 at the bottom of the list. For some the number of items in the list may be 40000+ so the count does not show

spoint

Upvotes: 0

Views: 821

Answers (1)

Amy Jiang_MSFT
Amy Jiang_MSFT

Reputation: 312

I found a same post may help you: SP.View object does not contain any methods for manipulating list items. But SP.View object contains SP.View.viewQuery property that specifies the query that is used by the list view. That means the following approach could be used for retrieving list items for view:

  • perform the first request to get CAML Query for List View using SP.View.viewQuery property
  • perform the second request to retrieve List Items by specifying CAML Query How to return list items for a List View using REST API using JavaScript
    function getJson(url) 
    {
        return $.ajax({       
           url: url,   
           type: "GET",  
           contentType: "application/json;odata=verbose",
           headers: { 
              "Accept": "application/json;odata=verbose"
           }
        });
        }
        
        
    function getListItems(webUrl,listTitle, queryText) 
    {
        var viewXml = '<View><Query>' + queryText + '</Query></View>';
        var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems"; 
        var queryPayload = {  
                   'query' : {
                          '__metadata': { 'type': 'SP.CamlQuery' }, 
                          'ViewXml' : viewXml  
                   }
        };
    
    return $.ajax({
           url: url,
           method: "POST",
           data: JSON.stringify(queryPayload),
           headers: {
              "X-RequestDigest": $("#__REQUESTDIGEST").val(),
              "Accept": "application/json; odata=verbose",
              "content-type": "application/json; odata=verbose"
           }
     });
    
    }
    
    
    function getListItemsForView(webUrl,listTitle,viewTitle)
    {
         var viewQueryUrl = webUrl + "/_api/web/lists/getByTitle('" + listTitle + "')/Views/getbytitle('" + viewTitle + "')/ViewQuery";
         return getJson(viewQueryUrl).then(
             function(data){         
                 var viewQuery = data.d.ViewQuery;
                 return getListItems(webUrl,listTitle,viewQuery); 
             });
    }

Usage

getListItemsForView(_spPageContextInfo.webAbsoluteUrl,'Announcements','Latest News')
.done(function(data)
{
     var items = data.d.results;
     for(var i = 0; i < items.length;i++) {
         console.log(items[i].Title);
     }    
})
.fail(
function(error){
    console.log(JSON.stringify(error));
});

Reference:

Using REST to fetch SharePoint View Items

How to get the total number of Items in a specific view in a list using rest api

Upvotes: 0

Related Questions