使用开放扩展向用户添加自定义数据Add custom data to users using open extensions

我们将引导你完成一个示例,演示如何使用开放扩展We're going to walk you through an example to demonstrate how to use open extensions.

假设你正在构建一个可在许多不同客户端平台(例如桌面和移动设备)上使用的应用程序。你希望让用户配置自己的 UI 体验,这样无论用户使用何种设备登录你的应用都能获得相同的体验。这是对大多数应用的常见要求。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, we're going to show you how to:

  1. 添加代表有关用户的一些漫游配置文件信息的开放扩展。Add an open extension representing some roaming profile information about the user.
  2. 查询用户并返回漫游配置文件。Query the user and return the roaming profile.
  3. 更改用户的漫游配置文件信息(开放扩展值)。Change the user's roaming profile information (the open extension value).
  4. 删除用户的漫游配置文件信息。Delete the user's roaming profile information.

注意: 本主题介绍如何在用户资源上添加、读取、更新和删除开放扩展。administrativeUnitcontactdeviceeventgroupgroup eventgroup postorganizaton 资源类型也支持这些方法Note: This topic shows you how to add, read, update and delete open extensions on a user resource. These methods are also supported for the administrativeUnit, contact, device, event, group, group event, group post and organizaton resource types.
只需使用这些资源类型中的任意一种更新下面的示例请求即可。简便起见,将下面示例中所示的响应截断。Simply update the example requests below using any of those resource types. The responses shown in the examples below may be truncated for brevity.

1.添加漫游配置文件信息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.

请求Request
POST https://graph.microsoft.com/v1.0/me/extensions
Content-type: application/json
{
    "@odata.type":"microsoft.graph.openTypeExtension",
    "extensionName":"com.contoso.roamingSettings",
    "theme":"dark",
    "color":"purple",
    "lang":"Japanese"
}
响应Response
HTTP/1.1 201 Created
Content-Type: application/json
Content-length: 420

{
    "@odata.type": "#microsoft.graph.openTypeExtension",
    "extensionName": "com.contoso.roamingSettings",
    "id": "com.contoso.roamingSettings",
    "theme": "dark",
    "color": "purple",
    "lang": "Japanese"
}

2.检索漫游配置文件信息2. Retrieve roaming profile information

当用户从其他设备登录应用时,该应用可以检索用户配置文件详细信息及其漫游设置。这可以通过获取用户的资源,以及对扩展导航属性进行扩展的方式完成。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.

请求Request
GET https://graph.microsoft.com/v1.0/me?$select=id,displayName,mail,mobilePhone&$expand=extensions
响应Response
HTTP/1.1 200 OK
Content-Type: application/json
Content-length: 420

{
    "id": "84b80893-8749-40a3-97b7-68513b600544",
    "displayName": "John Smith",
    "mail": "john@contoso.com",
    "mobilePhone": "1-555-6589",
    "extensions": [
        {
            "@odata.type": "#microsoft.graph.openTypeExtension",
            "extensionName": "com.contoso.roamingSettings",
            "id": "com.contoso.roamingSettings",
            "theme": "dark",
            "color": "purple",
            "lang": "Japanese"
        }
    ]
}

注意: 如果你有多个扩展,则可以按 ID 筛选,以获取感兴趣的扩展。Note: If you have multiple extensions, you can filter on the id to get the extension that you are interested in.

3.更改漫游配置文件信息3. Change roaming profile information

用户可以选择更改其漫游配置文件信息。此更新可以通过开放扩展值上的 PATCH 完成。The user may choose to change their roaming profile information. This update can be done with a PATCH on the open extension value.

请求Request
PATCH https://graph.microsoft.com/v1.0/me/extensions/com.contoso.roamingSettings
Content-type: application/json
{
    "theme":"light",
    "color":"yellow",
    "lang":"Swahili"
}
响应Response
HTTP/1.1 204 No content

4.删除用户漫游配置文件4. Delete a user's roaming profile

用户决定不再使用漫游配置文件,因此将其删除。这可以通过开放扩展值上的 DELETE 请求完成。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.

请求Request
DELETE https://graph.microsoft.com/v1.0/me/extensions/com.contoso.roamingSettings
响应Response
HTTP/1.1 204 No content

另请参阅See also