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
var graphClient = new GraphServiceClient(requestAdapter);
var requestBody = new OpenTypeExtension
{
OdataType = "microsoft.graph.openTypeExtension",
ExtensionName = "com.contoso.roamingSettings",
AdditionalData = new Dictionary<string, object>
{
{
"theme" , "dark"
},
{
"color" , "purple"
},
{
"lang" , "Japanese"
},
},
};
var result = await graphClient.Me.Extensions.PostAsync(requestBody);
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$requestBody = new OpenTypeExtension();
$requestBody->set@odatatype('microsoft.graph.openTypeExtension');
$requestBody->setExtensionName('com.contoso.roamingSettings');
$additionalData = [
'theme' => 'dark',
'color' => 'purple',
'lang' => 'Japanese',
];
$requestBody->setAdditionalData($additionalData);
$result = $graphServiceClient->me()->extensions()->post($requestBody);
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
var graphClient = new GraphServiceClient(requestAdapter);
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 FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$requestConfiguration = new MeRequestBuilderGetRequestConfiguration();
$queryParameters = MeRequestBuilderGetRequestConfiguration::createQueryParameters();
$queryParameters->select = ["id","displayName","mail","mobilePhone"];
$queryParameters->expand = ["extensions"];
$requestConfiguration->queryParameters = $queryParameters;
$result = $graphServiceClient->me()->get($requestConfiguration);
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
var graphClient = new GraphServiceClient(requestAdapter);
var requestBody = new Extension
{
AdditionalData = new Dictionary<string, object>
{
{
"theme" , "light"
},
{
"color" , "yellow"
},
{
"lang" , "Swahili"
},
},
};
var result = await graphClient.Me.Extensions["{extension-id}"].PatchAsync(requestBody);
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$requestBody = new Extension();
$additionalData = [
'theme' => 'light',
'color' => 'yellow',
'lang' => 'Swahili',
];
$requestBody->setAdditionalData($additionalData);
$result = $graphServiceClient->me()->extensions()->byExtensionId('extension-id')->patch($requestBody);
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
var graphClient = new GraphServiceClient(requestAdapter);
await graphClient.Me.Extensions["{extension-id}"].DeleteAsync();
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$graphServiceClient->me()->extensions()->byExtensionId('extension-id')->delete();
// THE PYTHON SDK IS IN PREVIEW. NON-PRODUCTION USE ONLY
client = GraphServiceClient(request_adapter)
await client.me.extensions.by_extension_id('extension-id').delete()