1

I have requirement to get the total number of items in a specific view in a SharePoint list.

I am trying below end point but it is returning count of all item in a list.

/_api/Web/Lists/GetByTitle('<list_name>')/Items

let say view name is XYZ and list name is ABC how to build a rest api to get total count of items in XYZ view from ABC list?

raj parihar
  • 25
  • 1
  • 7

2 Answers2

1

You can do it either way, using CAML Query and using same filters as list view or using filters in your Rest API URL. For details please check https://sharepoint.stackexchange.com/a/266795/68021

Muhammad Murad Haider
  • 1,357
  • 2
  • 18
  • 34
1

To get the item count in a specific list view, firstly get the list view CAML Query, then use this CAML Query with Post Request in Rest API to return items, here is a code snippet for your reference:

<script type="text/javascript">
 getListItemsForView(_spPageContextInfo.webAbsoluteUrl,'ABC','XYZ')
    .done(function(data)
    {
         var itemsCount = data.d.results.length;
         alert(itemsCount);         

    })
    .fail(
    function(error){
        console.log(JSON.stringify(error));
    });



 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); 
             });
    }

    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"
               }
         });
    }
</script>

Reference:

Using REST to fetch SharePoint View Items

Jerry
  • 3,480
  • 1
  • 10
  • 12