Namespace: microsoft.graph
Set the state of a user's presence session as an application.
This API is available in the following national cloud deployments.
Global service |
US Government L4 |
US Government L5 (DOD) |
China operated by 21Vianet |
✅ |
✅ |
✅ |
❌ |
Presence sessions
A user can have multiple presence sessions because the user can be on multiple Teams clients (desktop, mobile, and web). Each Teams client has an independent presence session and the user's presence is an aggregated state from all the sessions behind.
Similarly, an application can have its own presence session for a user and be able to update the state.
The following precedence is used for how session states are aggregated:
- User-configured > app-configured (user-configured state overrides others)
- Among app-configured: DoNotDisturb > Busy > Available > Away
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.
Timeout, expiration, and keep alive
A presence session may time out and expire, so the application needs to call this API before the timeout, to maintain the state for the session; or before the expiration, to keep the session alive.
A presence session can time out if the availability is Available
and the timeout is 5 minutes. When it times out, the presence state fades in stages. For example, if an application sets the presence session as Available/Available
, the state would change to Available/AvailableInactive
in 5 minutes with the first timeout, then Away/Away
in another 5 minutes with the second timeout.
Use expirationDuration
to configure the expiration of a presence session; otherwise, the default expiration is 5 minutes. Valid values range from 5 minutes to 4 hours, after which the session becomes Offline
.
Permissions
Choose the permission or permissions marked as least privileged for this API. Use a higher privileged permission or permissions only if your app requires it. For details about delegated and application permissions, see Permission types. To learn more about these permissions, see the permissions reference.
Permission type |
Least privileged permissions |
Higher privileged permissions |
Delegated (work or school account) |
Presence.ReadWrite |
Not available. |
Delegated (personal Microsoft account) |
Not supported. |
Not supported. |
Application |
Presence.ReadWrite.All |
Not available. |
HTTP Request
POST /users/{userId}/presence/setPresence
Request body
In the request body, provide a JSON object with the following parameters.
Parameter |
Type |
Description |
sessionId |
string |
The ID of the application's presence session. |
availability |
string |
The base presence information. |
activity |
string |
The supplemental information to availability. |
expirationDuration |
duration |
The expiration of the app presence session. The value is represented in ISO 8601 format for durations.If not provided, a default expiration of 5 minutes is applied. The valid duration range is from 5 to 240 minutes (PT5M to PT4H). |
Important
Provide the ID of the application as sessionId
in the request.
Supported combinations of availability
and activity
are:
availability |
activity |
Description |
Available |
Available |
Updates the presence session as Available. |
Busy |
InACall |
Updates the presence session as Busy, InACall. |
Busy |
InAConferenceCall |
Updates the presence session as Busy, InAConferenceCall. |
Away |
Away |
Updates the presence session as Away. |
DoNotDisturb |
Presenting |
Updates the presence session as DoNotDisturb, Presenting. |
Response
If successful, this method returns a 200 OK
response code.
Examples
The following request shows the application with ID 22553876-f5ab-4529-bffb-cfe50aa89f87
that sets its presence session for user fa8bf3dc-eca7-46b7-bad1-db199b62afc3
.
Request
POST https://graph.microsoft.com/v1.0/users/fa8bf3dc-eca7-46b7-bad1-db199b62afc3/presence/setPresence
Content-Type: application/json
{
"sessionId": "22553876-f5ab-4529-bffb-cfe50aa89f87",
"availability": "Available",
"activity": "Available",
"expirationDuration": "PT1H"
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Users.Item.Presence.SetPresence;
var requestBody = new SetPresencePostRequestBody
{
SessionId = "22553876-f5ab-4529-bffb-cfe50aa89f87",
Availability = "Available",
Activity = "Available",
ExpirationDuration = TimeSpan.Parse("PT1H"),
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
await graphClient.Users["{user-id}"].Presence.SetPresence.PostAsync(requestBody);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
mgc users presence set-presence post --user-id {user-id} --body '{\
"sessionId": "22553876-f5ab-4529-bffb-cfe50aa89f87",\
"availability": "Available",\
"activity": "Available",\
"expirationDuration": "PT1H"\
}\
'
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
abstractions "github.com/microsoft/kiota-abstractions-go"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphusers "github.com/microsoftgraph/msgraph-sdk-go/users"
//other-imports
)
requestBody := graphusers.NewSetPresencePostRequestBody()
sessionId := "22553876-f5ab-4529-bffb-cfe50aa89f87"
requestBody.SetSessionId(&sessionId)
availability := "Available"
requestBody.SetAvailability(&availability)
activity := "Available"
requestBody.SetActivity(&activity)
expirationDuration , err := abstractions.ParseISODuration("PT1H")
requestBody.SetExpirationDuration(&expirationDuration)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
graphClient.Users().ByUserId("user-id").Presence().SetPresence().Post(context.Background(), requestBody, nil)
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.users.item.presence.setpresence.SetPresencePostRequestBody setPresencePostRequestBody = new com.microsoft.graph.users.item.presence.setpresence.SetPresencePostRequestBody();
setPresencePostRequestBody.setSessionId("22553876-f5ab-4529-bffb-cfe50aa89f87");
setPresencePostRequestBody.setAvailability("Available");
setPresencePostRequestBody.setActivity("Available");
PeriodAndDuration expirationDuration = PeriodAndDuration.ofDuration(Duration.parse("PT1H"));
setPresencePostRequestBody.setExpirationDuration(expirationDuration);
graphClient.users().byUserId("{user-id}").presence().setPresence().post(setPresencePostRequestBody);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
const options = {
authProvider,
};
const client = Client.init(options);
const setPresence = {
sessionId: '22553876-f5ab-4529-bffb-cfe50aa89f87',
availability: 'Available',
activity: 'Available',
expirationDuration: 'PT1H'
};
await client.api('/users/fa8bf3dc-eca7-46b7-bad1-db199b62afc3/presence/setPresence')
.post(setPresence);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Users\Item\Presence\SetPresence\SetPresencePostRequestBody;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new SetPresencePostRequestBody();
$requestBody->setSessionId('22553876-f5ab-4529-bffb-cfe50aa89f87');
$requestBody->setAvailability('Available');
$requestBody->setActivity('Available');
$requestBody->setExpirationDuration(new \DateInterval('PT1H'));
$graphServiceClient->users()->byUserId('user-id')->presence()->setPresence()->post($requestBody)->wait();
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
Import-Module Microsoft.Graph.CloudCommunications
$params = @{
sessionId = "22553876-f5ab-4529-bffb-cfe50aa89f87"
availability = "Available"
activity = "Available"
expirationDuration = "PT1H"
}
Set-MgUserPresence -UserId $userId -BodyParameter $params
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.users.item.presence.set_presence.set_presence_post_request_body import SetPresencePostRequestBody
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = SetPresencePostRequestBody(
session_id = "22553876-f5ab-4529-bffb-cfe50aa89f87",
availability = "Available",
activity = "Available",
expiration_duration = "PT1H",
)
await graph_client.users.by_user_id('user-id').presence.set_presence.post(request_body)
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
Response
HTTP/1.1 200 OK