1

I am trying to get count of items in a view of a custom list using SharePoint server 2016.I am calling web API for it.

Web api:

<site_url>/sites/DMS/_api/Web/Lists(guid'{xxxxxx-xxx-xxxx-xxx-xxxx}-{xxxx-xxxx-xxxx-xxxx-xxxxxxx}')

But this leads to Microsoft.SharePoint.Client.InvalidClientQueryException. I tried using filter query too.

How can I get count of items of a view of a custom list using rest API .

  • Do you need all list items count or count of list items in particular View? Is there any particular reason you want to use REST? because you can get this OOTB also in SharePoint list view. – Ganesh Sanap Jul 24 at 12:35
  • I want count of list items in particular view.I am new to sharepoint.I got one suggestion to use REST as I want those counts in another separate list.How can I get count using either approach? – user85213 Jul 24 at 12:58
  • Are there any filter conditions on your list views? – Ganesh Sanap Jul 24 at 13:38
  • This will help you - stackoverflow.com/questions/26884647/… – Rohit Waghela Jul 24 at 13:39
  • 1
    @RohitWaghela It helped – user85213 Jul 24 at 14:17
0

I will suggest you 2 ways to achieve something similar for your requirements:

Solution 1:

You can get the list items from particular list view using CAML Query as given below:

Using REST to fetch SharePoint View Items.

And data.d.results.length will give you the item count.

Solution 2:

You can get list items using REST by adding your list view filter conditions in REST query(Depends on how complex filter queries you are using on views):

siteUrl/_api/web/Lists/getbytitle('List Name')/items?$select=Title,ID&$filter=ID eq 1

And data.d.results.length will give you the item count.

  • This solved my problem.Thank you – user85213 Jul 24 at 14:16
  • Welcome. Please upvote and accept the answer. It will help others to find correct answer easily. – Ganesh Sanap Jul 24 at 14:22
  • I got value using this approach.Now I am assigning it to to columns using fd.field('<column name>').value('Default title') in a script editor.But I am getting error as fd is not defined.So is there any other approach to assign value to a column? – user85213 Jul 25 at 6:35
  • Where are you assigning this column values? In another list form? Can you please Ask new question and mention your scenario there. Also, add the code you are using to set the field in that question. That will be easy to understand. – Ganesh Sanap Jul 25 at 6:39
1

Below how you can achieve in REST API.

First you need to make call to your targeted view,

/sites/mysc/_api/Web/Lists(@v0)/Views(@v1)?&@v0=guid'4fc236a-4364-4523e-a523a-abd90e0e0583'&@v1=guid'f0e92334-6c47-4223-9d1a-6b7ea5d239ada'

Here above v0 should your list GUID and v1 should be your view guid.

Once you make call to above service, you will get CAML Query associated with this view.

it would be available in ViewQuery attribute. For e.g data.d.ViewQuery

I get below output for my query

ViewQuery: "<OrderBy><FieldRef Name="ID" /></OrderBy>"

Keep this query in some variable say 'caml'.

Next thing to do is call REST API based on above CAML Query. Here we would use Post method and pass 'data'. Ref link

var data = { "query" :{"__metadata": { "type": "SP.CamlQuery" }, "ViewXml": caml}};

Now if you are using ajax to make REST Call, call would be something like below.

$.ajax({  
       url: siteUrl+"/_api/web/lists/GetByTitle('"+ listName +"')/GetItems",  
            method: "POST",  
            data: data,  
            headers: {  
               "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
               'content-type': 'application/json;odata=verbose',  
               'accept': 'application/json;odata=verbose'  
            }  
            success: function (response) {  
            ///do your code  
            },  
            error: function (data) {  
            ///do your code  
           }  
    });  
0

Using REST you can't get items from a view. For that you have two options

  1. Get the view fields and form a query
  2. Create a CAML query and use it to get the items.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.