This article walks you through an example to demonstrate how to use open extensions.
Imagine you're building an application that is available on lots of different 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. This is a common requirement for most apps.
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
This topic shows you how to add, read, update and delete open extensions on a user resource. 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. Here we'll see how 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 can retrieve the user's profile details as well as their roaming settings. This can be done by getting the user's resource and expanding the extension navigation property.
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 user decides that they don't want a roaming profile anymore, so they delete it. This can be done with a DELETE request on the open extension value.
//THE GO SDK IS IN PREVIEW. NON-PRODUCTION USE ONLY
graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)
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();