如何使用 rest api 获取列表中特定视图中的项目总数

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

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?

您可以采用任何一种方式,使用 CAML 查询并使用与列表视图相同的过滤器,或者使用您的 Rest API URL 中的过滤器。 详情请查看https://sharepoint.stackexchange.com/a/266795/68021

要获取特定列表视图中的项目计数,首先获取列表视图 CAML 查询,然后将此 CAML 查询与 Post 静态请求一起使用 API 到 return 项目,这里有一段代码供您参考:

<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>

参考:

Using REST to fetch SharePoint View Items