Microsoft Graph Presence API

Featured image

In Microsoft Teams anyone can see other user’s availability status(presence) in the organization. Previously, there is no API call to access user’s presence information. Many clients have requested this feature in Graph. Now Microsoft Graph supports the Presence API in the beta version.

What is Presence?

Presence is part of a user’s profile in Microsoft Teams that indicates the user’s current availability and status to other users. Presence details are based on user’s availability and user’s activity.

Presence API

Presence Resource Type has of following methods

Presence Resource Type consists of following properties

To access Presence API calls, admin consent is needed. The Permissions needed are

Get Presence API call

Permission: Presence.Read, Presence.Read.All

Request Method: GET

Request API url: GET https://graph.microsoft.com/beta/users/ {userid}/presence

Retrieve specific user status by mentioning user id in request call.

Response:

HTTP/1.1 200 OK
Content-Type: application/json

{
    "id": "668xxxxx-xxxx-xxxx-xxxx-xxxxxxx",
    "availability": "Busy",
    "activity": "InACall"
}

Get multiple users Presence API call

Permission: Presence.Read.All

Request: Here we use Microsoft Graph Communication API to get multiple users status

Request Method: POST

Request API url: https://graph.microsoft.com/beta/communications/getPresencesByUserId

Request body: Mention the user id separated by comma

{
    "ids": ["as3wxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "jh5rxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"]
}

Response:

HTTP/1.1 200 OK
Content-Type: application/json
{
    "value": [{
            "id": "as3wxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
            "availability": "Busy",
            "activity": "InACall"
        },
        {
            "id": "jh5rxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
            "availability": "Available",
            "activity": "Available"
        }
    ]
}


Export Teams Presence of all users using PowerShell Script

This PowerShell script export details of Microsoft Teams Presence in your tenant with the following information to a csv file

Prerequisites:

To run this script, you need to install SharePoint Online PnP module.

HTTP/1.1 200 OK
Content-Type: application/json

{
    
function Export-PresenceList
{  
    param (  
                  $ExportPath
                 )  
    process
    {
        Connect-PnPOnline -Scopes "Group.Read.All","User.ReadBasic.All","Presence.Read","Presence.Read.All"
        $accesstoken =Get-PnPAccessToken
        Connect-MsolService
        $users= Get-MsolUser -all |select ObjectId
        $userslist = $users.ObjectId
        $body = @{"ids" = $userslist } | ConvertTo-Json
        $header = @{
                                  "Authorization"="Bearer $accesstoken"
                                  "Content-Type"="application/json"
                               } 
        $presenceinfo = Invoke-RestMethod -Headers $header -Uri 
        https://graph.microsoft.com/beta/communications/getPresencesByUserId -Body $body -Method POST
        $presenceinfodetails= @()
        foreach($presence in $presenceinfo.value )
        {
            $user=get-msoluser -ObjectId $presence.id |select DisplayName,UserPrincipalName
            $userinfo = "" |select "UserId","Displayname","Emailaddress","availability", "activity"
            $userinfo.UserID = $presence.id
            $userinfo.Displayname = $user.DisplayName
            $userinfo.Emailaddress = $user.UserPrincipalName 
            $userinfo.availability =$presence.availability
            $userinfo.activity =$presence.activity
            $presenceinfodetails+= $userinfo
            $userinfo =$null
        }
        $presenceinfodetails | Export-csv $ExportPath -NoTypeInformation
    }
}
Export-PresenceList -ExportPath "C:\temp\teamspresencelist.csv"



}

Result:

Now using Presence API and script, we retrieved the user’s availability successfully.