Ajout de données personnalisées à des utilisateurs à l’aide des extensions d’ouverture
Article
Dans ce tutoriel, vous allez vous pencher sur l’utilisation des extensions ouvertes.
Imaginez que vous créez une application disponible sur plusieurs plateformes clientes, telles que les ordinateurs de bureau et les appareils mobiles. Vous souhaitez permettre aux utilisateurs de configurer leur expérience d’interface utilisateur afin qu’elle soit cohérente, quel que soit l’appareil qu’ils utilisent pour se connecter à votre application.
Pour ce scénario, cet article vous montre comment :
Représentez des informations de profil itinérant sur l’utilisateur à l’aide d’extensions ouvertes.
Interroger l’utilisateur et renvoyer le profil d’itinérance.
Modifiez les informations de profil itinérant de l’utilisateur stockées dans l’extension ouverte.
Supprimer les informations de profil d’itinérance de l’utilisateur.
Remarque
Outre les utilisateurs, les extensions ouvertes sont également prises en charge et peuvent être gérées pour d’autres types de ressources.
Configuration requise
Pour reproduire les étapes décrites dans cet article, vous avez besoin des privilèges suivants :
Connectez-vous à un client API tel que Graph Explorer et à l’utilisateur pour lequel vous souhaitez stocker le profil itinérant.
Accordez à l’application l’autorisation déléguée User.ReadWrite pour l’utilisateur connecté.
Étape 1. Ajouter des informations de profil itinérant
L’utilisateur se connecte à l’application et configure l’apparence de l’application. Ces paramètres d’application doivent être itinérants afin de garantir la même expérience à l’utilisateur sur n’importe quel appareil utilisé pour se connecter à l’application. L’application appelle Microsoft Graph en exécutant la requête suivante pour ajouter les informations de profil itinérant à l’objet de ressource de l’utilisateur connecté.
// 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);
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
//other-imports
)
requestBody := graphmodels.NewExtension()
extensionName := "com.contoso.roamingSettings"
requestBody.SetExtensionName(&extensionName)
additionalData := map[string]interface{}{
"theme" : "dark",
"color" : "purple",
"lang" : "Japanese",
}
requestBody.SetAdditionalData(additionalData)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
extensions, err := graphClient.Me().Extensions().Post(context.Background(), requestBody, nil)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
OpenTypeExtension extension = new OpenTypeExtension();
extension.setOdataType("microsoft.graph.openTypeExtension");
extension.setExtensionName("com.contoso.roamingSettings");
HashMap<String, Object> additionalData = new HashMap<String, Object>();
additionalData.put("theme", "dark");
additionalData.put("color", "purple");
additionalData.put("lang", "Japanese");
extension.setAdditionalData(additionalData);
Extension result = graphClient.me().extensions().post(extension);
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
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.models.open_type_extension import OpenTypeExtension
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = OpenTypeExtension(
odata_type = "microsoft.graph.openTypeExtension",
extension_name = "com.contoso.roamingSettings",
additional_data = {
"theme" : "dark",
"color" : "purple",
"lang" : "Japanese",
}
)
result = await graph_client.me.extensions.post(request_body)
Étape 2. Récupérer les informations de profil itinérant
Lorsque l’utilisateur se connecte à l’application à partir d’un autre appareil, l’application appelle Microsoft Graph pour récupérer les détails du profil de l’utilisateur et développer la propriété de navigation des extensions pour obtenir ses paramètres d’itinérance, puis utilise ces données pour fournir la même expérience que sur l’autre appareil.
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" };
});
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphusers "github.com/microsoftgraph/msgraph-sdk-go/users"
//other-imports
)
requestParameters := &graphusers.MeRequestBuilderGetQueryParameters{
Select: [] string {"id","displayName","mail","mobilePhone"},
Expand: [] string {"extensions"},
}
configuration := &graphusers.MeRequestBuilderGetRequestConfiguration{
QueryParameters: requestParameters,
}
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
me, err := graphClient.Me().Get(context.Background(), configuration)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
User result = graphClient.me().get(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"
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.me.me_request_builder import MeRequestBuilder
from kiota_abstractions.base_request_configuration import RequestConfiguration
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
query_params = MeRequestBuilder.MeRequestBuilderGetQueryParameters(
select = ["id","displayName","mail","mobilePhone"],
expand = ["extensions"],
)
request_configuration = RequestConfiguration(
query_parameters = query_params,
)
result = await graph_client.me.get(request_configuration = request_configuration)
HTTP/1.1 200 OK
Content-Type: application/json
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users(id,displayName,mail,mobilePhone,extensions())/$entity",
"@microsoft.graph.tips": "Use $select to choose only the properties your app needs, as this can lead to performance improvements. For example: GET me?$select=signInActivity,accountEnabled",
"id": "376bdbfc-e41f-4082-a8cf-b31731465eeb",
"displayName": "Raul Razo",
"mail": null,
"mobilePhone": null,
"extensions@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('376bdbfc-e41f-4082-a8cf-b31731465eeb')/extensions",
"extensions": [
{
"@odata.type": "#microsoft.graph.openTypeExtension",
"extensionName": "com.contoso.roamingSettings",
"theme": "dark",
"color": "purple",
"lang": "Japanese",
"id": "com.contoso.roamingSettings"
}
]
}
Étape 3. Modifier les informations du profil d’itinérance
L’utilisateur peut choisir de modifier ses informations de profil d’itinérance. L’application appelle Microsoft Graph en exécutant la requête suivante. La requête retourne un code de réponse 204 No Content .
Vous devez également inclure toutes les propriétés dans le corps de la demande, même si vous souhaitez mettre à jour uniquement un sous-ensemble d’entre elles. Sinon, Microsoft Graph supprime les propriétés que vous ne transmettez pas. Pour supprimer des données tout en conservant une propriété, définissez la valeur de la propriété sur null.
// 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);
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
//other-imports
)
requestBody := graphmodels.NewExtension()
additionalData := map[string]interface{}{
"theme" : "light",
"color" : "yellow",
"lang" : "Swahili",
}
requestBody.SetAdditionalData(additionalData)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
extensions, err := graphClient.Me().Extensions().ByExtensionId("extension-id").Patch(context.Background(), requestBody, nil)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
Extension extension = new Extension();
HashMap<String, Object> additionalData = new HashMap<String, Object>();
additionalData.put("theme", "light");
additionalData.put("color", "yellow");
additionalData.put("lang", "Swahili");
extension.setAdditionalData(additionalData);
Extension result = graphClient.me().extensions().byExtensionId("{extension-id}").patch(extension);
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 1.x
from msgraph import GraphServiceClient
from msgraph.generated.models.extension import Extension
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = Extension(
additional_data = {
"theme" : "light",
"color" : "yellow",
"lang" : "Swahili",
}
)
result = await graph_client.me.extensions.by_extension_id('extension-id').patch(request_body)
Étape 4. Supprimer le profil itinérant d’un utilisateur
L’utilisateur décide qu’il ne veut plus d’un profil itinérant. Pour supprimer la propriété d’extension, l’application appelle Microsoft Graph en exécutant la requête suivante. La requête retourne un code de réponse 204 No Content .
// 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();
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
//other-imports
)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
graphClient.Me().Extensions().ByExtensionId("extension-id").Delete(context.Background(), nil)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
graphClient.me().extensions().byExtensionId("{extension-id}").delete();
<?php
use Microsoft\Graph\GraphServiceClient;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$graphServiceClient->me()->extensions()->byExtensionId('extension-id')->delete()->wait();
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
await graph_client.me.extensions.by_extension_id('extension-id').delete()