-1

I need the count of Status column in Sharepoint list. I have used React as the mode in spfx.

 @autobind
  private async _loadAsyncData(): Promise<Chart.ChartData> {
    const items: any[] = await sp.web.lists.getByTitle("Sales").items.select("Title", "Salesamt", "Status").get();
    let lblarr: string[] = [];
    let dataarr: number[] = [];
    items.forEach(element => {
      lblarr.push(element.Title);
      dataarr.push(element.Salesamt);
    });
    let chartdata: Chart.ChartData = {
      labels: lblarr,
      datasets: [{
        label: 'My data',
        data: dataarr
      }]
    };
    return chartdata;
  }

Can someone help me to get the count of items in the status column in the above code

Trisma
  • 735
  • 6
  • 19
  • Welcome to Stack Overflow! Please [take the tour](http://stackoverflow.com/tour) (you get a badge!) and read through the [help center](https://stackoverflow.com/help), in particular [good question?](https://stackoverflow.com/help/how-to-ask) Your best bet here is to do your research, [search](https://stackoverflow.com/help/searching) for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research, post a [Verifiable Example](http://stackoverflow.com/help/mcve) of your attempt and specifically say where you're stuck. People will be glad to help. – Jacob Aug 14 '20 at 17:41

1 Answers1

0

Hi Nilanjan Mukherjee,

If your list is not very large, you can consider enumerating the whole list.

Another way is to use RenderListData() + CAML/Aggregations

  1. Create a test list

enter image description here

  1. Use below PnP code to get the count (note that the count is 2 while the row number is 3)

  const caml: ICamlQuery = {
    ViewXml: `<View><ViewFields><FieldRef Name="Title"/><FieldRef Name="johnjohn"/></ViewFields><Aggregations Value="On"><FieldRef Name="johnjohn" Type="Count"/></Aggregations></View>`
  };

  const r = await sp.web.lists.getByTitle('mm').renderListData(caml.ViewXml);

  console.log(r);

Result:

enter image description here

Check below blog to get more details:

https://codeatwork.wordpress.com/2017/10/13/aggregation-using-caml-query/

BR

Baker_Kong
  • 1,739
  • 1
  • 4
  • 9
  • This fails when there are more than 5000 items in the list. How can I get the actual number of items for "big" lists? Regards Leif – Leif Jan 04 '21 at 10:23