【问题标题】:Using Graph API how to write Sharepoint "Person or Group" field?使用 Graph API 如何编写 Sharepoint“个人或组”字段?
【发布时间】:2020-01-03 20:20:38
【问题描述】:

我有一个 Sharepoint Online 列表,其中包含“个人或组”类型的列。我想使用 Microsoft Graph API(最终来自 Teams 机器人)为此类列创建一个新条目。

我的问题是这样的列包含一个查找值,我不知道如何获得这个数字。我可以成功地从 Graph(/me 或 /users)获取用户对象,但返回的对象有一个完全不同的 id。如何将此链接到 Sharepoint Online 所需的内容?

示例:Graph 返回的类型为“Person or Group”的 Sharepoint 列 SG_Responsible(注意报告为 SG_ResponsibleLookupId):

      "fields": {
                "@odata.etag": "\"e2977d91-fd6d-4141-8bc6-897f5bbe9840,1\"",
                "Title": "Test 2",
                "SG_ResponsibleLookupId": "12",
                "SG_Description": "This is a Test",
       ...

来自/users的对应用户:

        {
            "businessPhones": [],
            "displayName": "John Doe",
            "givenName": null,
            "jobTitle": null,
            "mail": null,
            "mobilePhone": null,
            "officeLocation": null,
            "preferredLanguage": null,
            "surname": null,
            "userPrincipalName": "john@example.com",
            "id": "76a3b5b7-768b-4dde-ba13-6d471aa2c7fd"
        },

那么如何确定这个用户是 Sharepoint 的用户 #12?

【问题讨论】:

    标签: microsoft-graph-api sharepoint-online


    【解决方案1】:

    用户 ID 是特定于网站集的,您可以通过 SharePoint REST api 获得,而根据我的测试没有用于此的图形 api。

    _api/web/siteusers
    

    在不工作时检查了this workaround

    【讨论】:

    • 非常感谢您的回答。你能详细说明第二部分吗?我不太明白。同时,我可以访问所有系统列表/v1.0/sites/root/lists?$select=system,id,name 并选择名为users 的列表的ID。不幸的是,我通过查询users 系统列表找到的用户在任何地方都没有与我必须输入到共享点列表中的内容相对应的 ID。
    • Graph 社区库在 v1.17.1 中增加了对 EnsureUser 的支持。 nuget.org/packages/Graph.Community/1.17.1-preview1 EnsureUser 返回的 Id 是设置查找 id 时使用的值。
    • 我已经很久没有这样做了,但ensureUser 是我最终使用的。
    【解决方案2】:

    SharePoint REST API 应该能够为您提供您正在工作的网站集中的用户 ID。

    用相应的值替换这些值。

    SharePointWebURL/_api/web/siteusers?$filter=Email eq 'UserEmail'&$select=Id

    然后您应该能够获取用户Id,您可以使用该用户来验证查找字段值。

    【讨论】:

      【解决方案3】:

      请查看以下指南: https://dev.to/manuelsidler/create-a-sharepoint-list-item-with-a-people-lookup-field-using-microsoft-graph-sdk-4ad

      它由 Manuel Sidler 编写,他使用 C# Graph SDK。

      解决方案

      Microsoft Graph 有一种方法。老实说,这有点古怪,但至少我们不必使用 SharePoint API 并留在我们的 Graph 气泡中。 首先,我们必须获取隐藏用户查找列表的 id:

      var hiddenUserListId = (await _graph
         .Sites["<siteid>"]
         .Lists
         .Request()
         .Filter("displayName eq 'User Information List'")
         .GetAsync())[0].Id;
      

      接下来,如果我们还没有用户主体名称,我们必须获取它:

      var userName = (await _graph
         .Users[userId]
         .Request()
         .Select("userPrincipalName")
         .GetAsync()).UserPrincipalName;
      

      有了用户主体名称,我们现在可以查询查找 ID:

      var userLookupId = (await _graph
         .Sites["<siteid>"]
         .Lists[hiddenUserListId]
         .Items
         .Request()
         .Header("Prefer", "HonorNonIndexedQueriesWarningMayFailRandomly")
         .Filter($"fields/UserName eq '{userName}'")
         .GetAsync())[0].Id;
      

      查询非索引列需要带有 HonorNonIndexedQueriesWarningMayFailRandomly 的首选标头。 最后,我们可以创建一个新的费用列表项:

      var expenseItem = new ListItem
      {
         Fields = new FieldValueSet
         {
             AdditionalData = new Dictionary<string, object>
             {
                 {"Title", "Train to Zurich"},
                 {"Amount", 23.2},
                 {"EmployeeLookupId", userLookupId}
             }
         }
      };
      await _graph
         .Sites["<siteid>"]
         .Lists["Expenses"]
         .Items
         .Request()
         .AddAsync(expenseItem);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-12
        • 2015-07-16
        相关资源
        最近更新 更多