使用spfx在Sharepoint列表中的项目计数

问题描述

我需要共享点列表中“状态”列的计数。我已经将React用作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;
  }

有人可以帮我获取上述代码的状态栏中的项目数吗?

解决方法

您好Nilanjan Mukherjee,

如果列表不是很大,可以考虑枚举整个列表。

另一种方法是使用RenderListData() + CAML/Aggregations

  1. 创建测试列表

enter image description here

  1. 使用下面的PnP代码获取计数(请注意,计数为2,而行号为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);

结果:

enter image description here

查看下面的博客以获取更多详细信息:

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

BR