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.
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
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$requestBody = new Extension();
$requestBody->set@odatatype('microsoft.graph.openTypeExtension');
$additionalData = [
'extensionName' => 'com.contoso.roamingSettings',
'theme' => 'dark',
'color' => 'purple',
'lang' => 'Japanese',
];
$requestBody->setAdditionalData($additionalData);
$requestResult = $graphServiceClient->me()->extensions()->post($requestBody);
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
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" };
});
Import-Module Microsoft.Graph.Users
# A UPN can also be used as -UserId.
Get-MgUser -UserId $userId -Property "id,displayName,mail,mobilePhone" -ExpandProperty "extensions"
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$requestConfiguration = new MeRequestBuilderGetRequestConfiguration();
$queryParameters = new MeRequestBuilderGetQueryParameters();
$queryParameters->select = ["id","displayName","mail","mobilePhone"];
$queryParameters->expand = ["extensions"];
$requestConfiguration->queryParameters = $queryParameters;
$requestResult = $graphServiceClient->me()->get($requestConfiguration);
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
<?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);
$requestResult = $graphServiceClient->me()->extensionsById('extension-id')->patch($requestBody);
//THE GO SDK IS IN PREVIEW. NON-PRODUCTION USE ONLY
graphClient := msgraphsdk.NewGraphServiceClientWithCredentials(cred, scopes)
graphClient.Me().ExtensionsById("extension-id").Delete(context.Background(), nil)
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$graphServiceClient->me()->extensionsById('extension-id')->delete();