Search code examples
azuremicrosoft-graph-apimicrosoft-teams

Presence not updating in Microsoft Teams using Graph API setUserPreferredPresence endpoint


We have developed an MS Teams Tab app. One of its features allows users to set their MS Teams status directly from our app. We use the setUserPreferredPresence endpoint of the Microsoft Graph API to update the status. While this functionality generally works well, we have encountered an issue when a user sets their status to "Away" in MS Teams and remains inactive for more than 15-20 minutes. In this scenario, when we call the Graph API to update the status using setUserPreferredPresence, we receive a 200 OK response from Graph, but user's status does not change in Teams.

Our API call to the Graph API looks like this: https://graph.microsoft.com/v1.0/users/${ids}/presence/setUserPreferredPresence. This works for other statuses but fails when trying to update from "Away."

any help would be appreciated. Thanks


Solution

  • As mentioned in this MS Doc,

    Preferred presence takes effect only when at least one presence session exists for the user. Otherwise, the user's presence shows as Offline.

    For presence session, you need to either create it using set presence API call or make sure user is active by logging into MS Teams client.

    I registered one application and added API permissions of Delegated type like this:

    enter image description here

    Initially, I created both tokens using authorization code flow via Postman like this:

    POST https://login.microsoftonline.com/tenantId/oauth2/v2.0/token
    grant_type:authorization_code
    client_id:appId
    client_secret:secret
    scope:Presence.ReadWrite offline_access
    code:code
    redirect_uri: https://jwt.ms
    

    Response:

    enter image description here

    Now, I used this refresh token to generate new access token after some time with below parameters:

    POST https://login.microsoftonline.com/tenantId/oauth2/v2.0/token
    grant_type:refresh_token
    client_id:appID
    client_secret:client_secret
    refresh_token: 0.AbcAmPlsIszdBUCs //paste the refresh token that you got above
    scope: Presence.ReadWrite
    

    Response:

    enter image description here

    In the case where user is not active for more than 15-20 minutes, make use of set presence API call first to create presence session like this:

    POST https://graph.microsoft.com/v1.0/users/userID/presence/setPresence
    Content-Type: application/json
    
    {
      "sessionId": "appId",
      "availability": "Away",
      "activity": "Away",
      "expirationDuration": "PT1H"
    }
    

    Response:

    enter image description here

    Note: When a user presence changes in Microsoft Graph, because the Teams client uses poll mode, it will take a few minutes to update the presence status.

    You can also run below API call to check the user's presence status after few minutes that changed to Away successfully like this:

    GET https://graph.microsoft.com/v1.0/users/userID/presence/
    

    Response:

    enter image description here

    Reference: presence: setPresence - Microsoft Graph v1.0