Get count of items/files in list/document library in SharePoint Online with SPFx

In SharePoint Online, when we view Site Contents, internally a call is made to the end point /_api/web/AppTiles to get the count of items/files in list/document library that the current site collection has. We can call this API in SPFx webpart to get these details. Below is the sample code.

import { SPHttpClient, SPHttpClientResponse, SPHttpClientConfiguration } from '@microsoft/sp-http';

this.context.spHttpClient.get(`${this.context.pageContext.web.absoluteUrl}/_api/web/AppTiles`, SPHttpClient.configurations.v1)
  .then((response: SPHttpClientResponse) => {
    response.json()
      .then((responseJSON: any) => {
        console.log(responseJSON); // Gets all the list and document library details in JSON
      })
      .catch(error => {
        console.log(error);
      });
  })
  .catch(error => {
    console.log(error);
  });

this.context refers to WebPartContext in the BaseClientSideWebPart class. The output will looks something like below JSON snippet. There is a lot of detail here which we can filter using $select. The attributes we require are Title and ChildCount.

{
    "@odata.context": "https://mysite.sharepoint.com/sites/mysitecollection/_api/$metadata#AppTiles",
    "value": [
      {
        "@odata.type": "#SP.AppTile",
        "@odata.id": "https://mysite.sharepoint.com/sites/mysitecollection/_api/SP.AppTile0aab34a5-8bb4-4d5e-bac0-90841a08dd34",
        "@odata.editLink": "SP.AppTile0aab34a5-8bb4-4d5e-bac0-90841a08dd34",
        "AppId": "58de69fc-9e3f-4375-af44-205eeb452366",
        "AppPrincipalId": "",
        "AppSource": 0,
        "AppStatus": 4,
        "AppType": 0,
        "AssetId": "0;00bfea23-e717-4e80-bb17-d0c71b360101;101;",
        "BaseTemplate": 101,
        "ChildCount": 5,
        "ContentMarket": "",
        "CustomSettingsUrl": "",
        "Description": "",
        "IsCorporateCatalogSite": false,
        "LastModified": "4/26/2020 4:16 AM",
        "LastModifiedDate": "2020-04-26T11:16:27Z",
        "ProductId": "00cfea20-e900-4e80-aa17-d0c71b360101",
        "Target": "/sites/mysitecollection/Shared Documents/Forms/AllItems.aspx",
        "Thumbnail": "/_layouts/15/images/ltdl.png?rev=47",
        "Title": "Documents",
        "Version": "4"
      },
      ...
    ]
  }

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply