What are the graph/rest API'S to get m365 size and item count details?

Yuvraj Patil 361 Reputation points
2024-07-21T16:45:50.14+00:00

Hello,

I am building an application where I want to show size and item count details for m365 workloads(SPO, exchange and teams). I would like to which are the graph/rest apis which I can use to get the current site as per below details.

  1. SPO: Api to get site collection total used storage and total item/file count.
  2. Exchange: Api to get total used storage and total item/mail count for a mail account as well as specific to primary, group, active, archived etc.
  3. Teams: Api to get site collection total used storage and total item count.

Thanks in advance!

Microsoft Exchange Online
OneDrive
OneDrive
A Microsoft file hosting and synchronization service.
971 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,265 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,805 questions
Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
3,059 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sayali-MSFT 2,266 Reputation points Microsoft Vendor
    2024-07-23T11:38:23.12+00:00

    To gather the required information for SharePoint Online (SPO), Exchange, and Teams, you can use Microsoft Graph and SharePoint REST APIs. Here are the APIs that will help you get the details :

    SharePoint Online (SPO)

    Get Site Collection Total Used Storage: Use the SharePoint REST API to get the site collection's storage details.

    Example:

    httpCopy code
    GET https://{tenant}.sharepoint.com/_api/site/usage
    

    Get Total Item/File Count: Use the Microsoft Graph API to get the item count in a document library.

    Example:

    httpCopy code
    GET https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/root/children
    

    Exchange

    Get Total Used Storage and Item/Mail Count for a Mail Account: Use the Microsoft Graph API to get the details of a user's mailbox.

    Example:

    httpCopy code
    GET https://graph.microsoft.com/v1.0/users/{user-id}/mailFolders/inbox
    

    To get details for specific folders like primary, group, active, archived, etc., you can iterate through the mail folders:

    httpCopy code
    GET https://graph.microsoft.com/v1.0/users/{user-id}/mailFolders
    

    Teams

    Get Site Collection Total Used Storage and Total Item Count: Since Teams relies on SharePoint for file storage, you can use the same SharePoint REST API and Microsoft Graph API methods as mentioned for SPO to get the storage and item count for Teams files stored in SharePoint.

    Example for Teams files in a SharePoint document library:

    httpCopy code
    GET https://graph.microsoft.com/v1.0/teams/{team-id}/channels/{channel-id}/filesFolder
    

    Permissions

    To access these APIs, you will need to register your application in Azure AD and grant the necessary permissions, such as:

    • Sites.Read.All (for SharePoint)
    • Mail.Read (for Exchange)
    • Group.Read.All (for Teams)

    Example Code

    Here is an example using Microsoft Graph SDK in C# to get mail folder details:

    csharpCopy code
    GraphServiceClient graphClient = 
    

    Replace {tenant}, {site-id}, {drive-id}, {user-id}, {team-id}, and {channel-id} with your actual values.

    Make sure to handle authentication and authorization properly to ensure your application can access the required data securely.