javascript - POSTing a new item to a SharePoint list using the MS Graph API with a People field - Stack Overflow

I have set up a React web app consuming data using Microsoft Graph from SharePoint Online which is work

I have set up a React web app consuming data using Microsoft Graph from SharePoint Online which is working well. I can read and write to SP Lists, but I am unable to find any documentation regarding how to structure the data to write into a 'People/Group' field.

Querying a list with these fields yields "ManagerLookupId": "12", where Manager is the People Picker field name. I can resolve the "12" by using the expand query string. I can't figure out how to write to it using either the /v1.0 or /beta endpoints from Microsoft Graph

Posting this object to the relevant endpoint works (for Single Line Text, Choice, etc).

{
  "fields": {
    "Title": "a",
    "Category": "b",
    "Description": "c"
  }
}

But if I were to add in the People picker field, I don't know how to structure that data. I have tried email address and GUID, both of which didn't work. Looking at how SP does it natively, it appears to submit this value on submission for the people picker field (have replaced real values) -

{
  "Key": "i:0#.f|membership|EMAIL",
  "DisplayText": "NAME",
  "IsResolved": true,
  "Description": "EMAIL",
  "EntityType": "User",
  "EntityData": {
    "IsAltSecIdPresent": "False",
    "Title": "POSITION",
    "Email": "EMAIL",
    "MobilePhone": "",
    "ObjectId": "GUID",
    "Department": ""
  },
  "MultipleMatches": [],
  "ProviderName": "Tenant",
  "ProviderDisplayName": "Tenant"
}

Replicating this also fails to update the People field in the list.

I wasn't able to find any documentation on the MS Graph API site or similar previous questions on their Github, so any help would be really appreciated!

I have set up a React web app consuming data using Microsoft Graph from SharePoint Online which is working well. I can read and write to SP Lists, but I am unable to find any documentation regarding how to structure the data to write into a 'People/Group' field.

Querying a list with these fields yields "ManagerLookupId": "12", where Manager is the People Picker field name. I can resolve the "12" by using the expand query string. I can't figure out how to write to it using either the /v1.0 or /beta endpoints from Microsoft Graph

Posting this object to the relevant endpoint works (for Single Line Text, Choice, etc).

{
  "fields": {
    "Title": "a",
    "Category": "b",
    "Description": "c"
  }
}

But if I were to add in the People picker field, I don't know how to structure that data. I have tried email address and GUID, both of which didn't work. Looking at how SP does it natively, it appears to submit this value on submission for the people picker field (have replaced real values) -

{
  "Key": "i:0#.f|membership|EMAIL",
  "DisplayText": "NAME",
  "IsResolved": true,
  "Description": "EMAIL",
  "EntityType": "User",
  "EntityData": {
    "IsAltSecIdPresent": "False",
    "Title": "POSITION",
    "Email": "EMAIL",
    "MobilePhone": "",
    "ObjectId": "GUID",
    "Department": ""
  },
  "MultipleMatches": [],
  "ProviderName": "Tenant",
  "ProviderDisplayName": "Tenant"
}

Replicating this also fails to update the People field in the list.

I wasn't able to find any documentation on the MS Graph API site or similar previous questions on their Github, so any help would be really appreciated!

Share Improve this question edited Jun 3, 2019 at 16:36 Marc LaFleur 33.2k4 gold badges40 silver badges70 bronze badges asked Jun 2, 2019 at 13:24 RonaldRonald 331 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You can find some information about this in the documentation for FieldValueSet:

Lookup fields (like Author above) are not returned by default. Instead, the server returns a 'LookupId' field (like AuthorLookupId above) referencing the listItem targeted in the lookup. The name of the 'LookupId' field is the original field name followed by LookupId.

Under the covers, this is a side-effect of how SharePoint tracks users. As an artifact of history, SharePoint maintains it's own User database. While there was a point in time when SP Admins had a lot of exposure to these users, today it's a fully automated process. It simply creates a new SPUser record the first time it sees an authenticated AAD User. These User records have their own id property. This is a mon INT rather than a GUID (in your example, their id is 12).

When you're updating a ListItem, you need to use SharePoint's id. You can pass this as a single value or as an array:

{
  "fields": {
     "ManagerLookupId": 12
  }
}

{
  "fields": {
     "ManagerLookupId": [10,11,12]
  }
}

Here is the rub, there is no way to obtain this SharePoint User id from Microsoft Graph. Obviously, this makes it really challenging to create/update a ListItem. To get around this, you can use the old SharePoint REST API's ensureUser endpoint:

http://<sitecollection>/<site>/_api/web/ensureUser(logonName)

Keep in mind, you'll also need a token for SharePoint to call this API. If you're using the AAD v1 Endpoint, this is generally just a matter of refreshing your token using your SharePoint resource (https://{tenant}.sharepoint.) for this call and then refreshing back to the Graph resource (https://graph.microsoft.) for your other calls.

My test demo, User is people field allow multiple users.

{
  "fields": {
    "Title": "test",
    "[email protected]": "Collection(Edm.Int32)",
    "UserLookupId":[10,23]
  }
}

For one user.

{
  "fields": {
    "Title": "test",
    "[email protected]": "Edm.Int32",
    "UserLookupId":23
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744978804a4604311.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信