Using Microsoft Graph Presence API on SharePoint Framework

Public preview of Microsoft Graph Presence API was published on December 2019. With current public preview, it is possible to get user’s own presence, presence of someone else and presence of multiple users with a single call. Getting a presence of signed-in user is retrieved by doing a HTTP GET call to either /beta/me/presence endpoint and /beta/users/<user- id>/presence endpoint for other users. Call returns a object with user’s availability and activity.

{
"@odata.context": "https://graph.microsoft.com/beta/$metadata#users('476594e4-ee42-4152-b464-c01966b50fe2')/presence/$entity",
"availability": "Away",
"activity": "Away",
"id": "476594e4-ee42-4152-b464-c01966b50fe2"
}

Getting a presence of multiple users is done by HTTP POST call to Microsoft Graph’s communication API presence endpoint /beta/communications/ getPresencesByUserId with body containing ids of users, like below.

{ 
"ids": ["fa8bf3dc-eca7-46b7-bad1-db199b62afc3", "66825e03-7ef5-42da-9069-724602c31f6b"] 
} 

And call returns an array user presence information.

 { 
"value": [
{ "id": "fa8bf3dc-eca7-46b7-bad1-db199b62afc3", "availability": "Busy", "activity": "InAMeeting" }, 
{ "id": "66825e03-7ef5-42da-9069-724602c31f6b", "availability": "Away", "activity": "Away" } ] 
} 

Possible returned values of availability are Available, AvailableIdle, Away, BeRightBack, Busy, BusyIdle, DoNotDisturb, Offline, PresenceUnknown and for activity AvailableAwayBeRightBack,BusyDoNotDisturbInACallInAConferenceCallInactive,InAMeetingOfflineOffWork,OutOfOfficePresenceUnknown,PresentingUrgentInterruptionsOnly

Currently beta API can be access only with user delegated permissions. Presence.Read permission allows reading presence of signed-in user and Presence.Read.All allows reading presence of all users in the directory.

More information about presence API can be found here: https://docs.microsoft.com/en-us/graph/api/resources/presence?view=graph-rest-beta

Example app – displaying presence of team members on Teams

Example app is a SharePoint Framework web part, which displays availability and activity of Office 365 Group members.

Availability is displayed as an ellipsis, where availablity values of the Graph API are mapped to Office UI Fabric’s PersonaPresence values, and activity is shown as text. Information is retrieved by making two Graph API calls. First needed group id is retrieved from legacyPageContext.

this.props.context.pageContext.legacyPageContext['groupId'];

First call is to get members of the current Office 365 Group by calling /groups/<group-id>/members endpoint. Returned member ids are the passed to presence endpoint of communications API, /communications/ getPresencesByUserId . Returned values are mapped with user information and displayed with Office UI Fabric’s Persona control.

Web part requires two API permissions to Microsoft Graph.

 "webApiPermissionRequests":
[{"resource":"Microsoft Graph","scope":"Presence.Read.All"},{"resource": "Microsoft Graph","scope": "Group.Read.All"}  ]  }

Example web part can be used on modern SharePoint page on Office 365 Group connected team sites and it can be added as a Microsoft Teams tab.

Afterword

Beta API already works nicely and status is updated almost instantly, when status is changed on Teams. Teams status is also the master. So when you set your status explicitly for example to “Be right back”, it’s returned until you change or reset your status. I also noticed that status based on calendar events is updated with some delay.

I didn’t test Skype for Business status, because I’m not using it anymore.

Source codes for the example web part can be found here: https://github.com/mpaukkon/SharePoint/tree/master/spfx-webparts/react-team-presence

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: