1

How to filter on choice column and get raw data count using SharePoint REST API?

// const url = `/_api/web/lists/getByTitle('${listTitle}')/items?${countQuery}`;

https://contoso.sharepoint.com/sites/IDEE/_vti_bin/ListData.svc/EInfochips/items?$filter=Department%20eq%20%27Admin%27
5
  • Hi @vinia, did you try my solution given below for choice column? Is it working for you in case of choice columns? Jun 7 at 8:25
  • @GaneshSanap yes working but want count directly from API
    – vinia shah
    Jun 7 at 8:28
  • 1
    As per my understanding, you cannot get count directly from API in case you are using filter with API request. Jun 7 at 8:30
  • 1
    Please Upvote(^) and accept my below response as an Answer if it helped you in any way & it will help others with similar question in future to find the correct answer easily. Jun 7 at 8:36
  • Hi @vinia, any update on this thread? Jun 8 at 8:00

2 Answers 2

0

You have to use the SharePoint REST API endpoint like this:

https://contoso.sharepoint.com/sites/IDEE/_api/web/lists/GetByTitle('EInfochips')/items?$filter=Department eq 'Admin'

Then in the success function of API call, you can get the count of returned results.

Example 1:

$.ajax({
    url: "https://contoso.sharepoint.com/sites/IDEE/_api/web/lists/GetByTitle('EInfochips')/items?$filter=Department eq 'Admin'",
    type: "GET",
    headers:
    {
        "Accept": "application/json;odata=verbose"
    },
    success: function (data, status, xhr) {
        var dataresults = data.d.results;
        console.log("No. of results: " + dataresults.length);
    },
    error: function (xhr, status, error) {
        console.log("Error");
    }
});

Example 2:

let requestUrl = "https://contoso.sharepoint.com/sites/IDEE/_api/web/lists/GetByTitle('EInfochips')/items?$filter=Department eq 'Admin'"

this.context.spHttpClient.get(requestUrl, SPHttpClient.configurations.v1)
    .then((response: SPHttpClientResponse) => {
        if (response.ok) {
            response.json().then((responseJSON) => {
                if (responseJSON!=null && responseJSON.value!=null){
                    let items:any[] = responseJSON.value;
                    console.log(items.length);
                }
            });
        }
    });

Where Department is the internal name of your choice column. You can get the internal name of your column by following this article: How to find the Internal name of columns in SharePoint Online?

Check below article to know more about how you can use it in SPFx: How To Work With List Items In SPFx Using REST API - Retrieve List Data


Instead of SPHttpClient and regular SharePoint REST API calls, you can also use SP PnP JS library in SPFx for such requirements.

Example 3:

const allItems: any[] = await sp.web.lists.getByTitle("EInfochips").items.select("Title").filter("Department eq 'Admin'").getAll();
console.log(allItems.length);

Documentation: SP PnP JS - Working with list items

0

Construct the REST API endpoint URL for the specific list and column you want to filter on. The URL will look something like this:

https://siteUrl/_api/web/lists/getbytitle('ListName')/items?$filter=ColumnName eq 'ChoiceValue'

Replace siteUrl with the URL of your SharePoint site, ListName with the name of your SharePoint list, ColumnName with the name of your choice column, and ChoiceValue with the specific value you want to filter on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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