This article demonstrates how to use open extensions.
Imagine you're building an application that is available on multiple client platforms, such as desktop and mobile. You want to let users configure their UI experience so it’s consistent no matter which device they use to sign in to your app.
For this scenario, this article will show you how to:
Add an open extension representing some roaming profile information about the user.
Query the user and return the roaming profile.
Change the user's roaming profile information (the open extension value).
Delete the user's roaming profile information.
Note
Apart from users, open extensions are also supported and can be managed for other resource types.
1. Add roaming profile information
The user signs in to the app and configures the look and feel of the app. These app settings should roam so that the user gets the same experience on whatever device they sign in to the app from. The app calls Microsoft Graph by running the following request to add the roaming profile information to a user resource.
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Models;
var requestBody = new OpenTypeExtension
{
OdataType = "microsoft.graph.openTypeExtension",
ExtensionName = "com.contoso.roamingSettings",
AdditionalData = new Dictionary<string, object>
{
{
"theme" , "dark"
},
{
"color" , "purple"
},
{
"lang" , "Japanese"
},
},
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Me.Extensions.PostAsync(requestBody);
// THE CLI IS IN PREVIEW. NON-PRODUCTION USE ONLY
mgc users extensions create --user-id {user-id} --body '{\
"@odata.type":"microsoft.graph.openTypeExtension",\
"extensionName":"com.contoso.roamingSettings",\
"theme":"dark",\
"color":"purple",\
"lang":"Japanese"\
}\
'
<?php
// THIS SNIPPET IS A PREVIEW VERSION OF THE SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new OpenTypeExtension();
$requestBody->setOdataType('microsoft.graph.openTypeExtension');
$requestBody->setExtensionName('com.contoso.roamingSettings');
$additionalData = [
'theme' => 'dark',
'color' => 'purple',
'lang' => 'Japanese',
];
$requestBody->setAdditionalData($additionalData);
$result = $graphServiceClient->me()->extensions()->post($requestBody)->wait();
Import-Module Microsoft.Graph.Users
$params = @{
"@odata.type" = "microsoft.graph.openTypeExtension"
extensionName = "com.contoso.roamingSettings"
theme = "dark"
color = "purple"
lang = "Japanese"
}
# A UPN can also be used as -UserId.
New-MgUserExtension -UserId $userId -BodyParameter $params
When the user signs in to the app from another device, the app calls Microsoft Graph to retrieve the user's profile details and expand the extensions navigation property to get their roaming settings.
GET https://graph.microsoft.com/v1.0/me?$select=id,displayName,mail,mobilePhone&$expand=extensions
// Code snippets are only available for the latest version. Current version is 5.x
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Me.GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Select = new string []{ "id","displayName","mail","mobilePhone" };
requestConfiguration.QueryParameters.Expand = new string []{ "extensions" };
});
<?php
// THIS SNIPPET IS A PREVIEW VERSION OF THE SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestConfiguration = new UserItemRequestBuilderGetRequestConfiguration();
$queryParameters = UserItemRequestBuilderGetRequestConfiguration::createQueryParameters();
$queryParameters->select = ["id","displayName","mail","mobilePhone"];
$queryParameters->expand = ["extensions"];
$requestConfiguration->queryParameters = $queryParameters;
$result = $graphServiceClient->me()->get($requestConfiguration)->wait();
Import-Module Microsoft.Graph.Users
# A UPN can also be used as -UserId.
Get-MgUser -UserId $userId -Property "id,displayName,mail,mobilePhone" -ExpandProperty "extensions"
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Models;
var requestBody = new Extension
{
AdditionalData = new Dictionary<string, object>
{
{
"theme" , "light"
},
{
"color" , "yellow"
},
{
"lang" , "Swahili"
},
},
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Me.Extensions["{extension-id}"].PatchAsync(requestBody);
// THE CLI IS IN PREVIEW. NON-PRODUCTION USE ONLY
mgc users extensions patch --user-id {user-id} --extension-id {extension-id} --body '{\
"theme":"light",\
"color":"yellow",\
"lang":"Swahili"\
}\
'
<?php
// THIS SNIPPET IS A PREVIEW VERSION OF THE SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new Extension();
$additionalData = [
'theme' => 'light',
'color' => 'yellow',
'lang' => 'Swahili',
];
$requestBody->setAdditionalData($additionalData);
$result = $graphServiceClient->me()->extensions()->byExtensionId('extension-id')->patch($requestBody)->wait();
Import-Module Microsoft.Graph.Users
$params = @{
theme = "light"
color = "yellow"
lang = "Swahili"
}
# A UPN can also be used as -UserId.
Update-MgUserExtension -UserId $userId -ExtensionId $extensionId -BodyParameter $params
// Code snippets are only available for the latest version. Current version is 5.x
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
await graphClient.Me.Extensions["{extension-id}"].DeleteAsync();
<?php
// THIS SNIPPET IS A PREVIEW VERSION OF THE SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$graphServiceClient->me()->extensions()->byExtensionId('extension-id')->delete()->wait();
# THE PYTHON SDK IS IN PREVIEW. FOR NON-PRODUCTION USE ONLY
graph_client = GraphServiceClient(credentials, scopes)
await graph_client.me.extensions.by_extension_id('extension-id').delete()