AskYous
AskYous

Reputation: 4760

Get SharePoint User by Lookup ID using Microsoft Graph

I perform the following query in Microsoft Graph:

var results = await SPLists["<my-list-name>"]
                .Items
                .Request()
                .Expand("fields")
                .GetAsync(); 

It gets the list items of a list I have in SharePoint. One of the columns in that list is of type "Person or Group". In the response from Microsoft Graph, it returns that column data like this (for example):

{
    "LookupId": 335,
    "LookupValue": "John Doe"
}

How do I take advantage of this data? How do I get the full profile of person with ID 355 using the Microsoft Graph? I need the email address of the person returned. How do I get that using the Microsoft Graph SDK?

Upvotes: 2

Views: 5873

Answers (2)

Brent Matzelle
Brent Matzelle

Reputation: 4093

Below is a C# example that queries the hidden SharePoint "User Information List" and then pulls the user lookup ID and email address.

string siteId = "ADD_YOUR_SITE_ID_HERE";

// Query the "User Information List" to get the users info
var userInfoList = await graphClient.Sites[siteId].Lists.GetAsync((request) =>
{
    request.QueryParameters.Filter = $"DisplayName eq 'User Information List'";
});

if (userInfoList == null || userInfoList.Value == null || !userInfoList.Value.Any())
{
    Console.WriteLine($"User Information List not found");
    return;
}

// The first record is the User Information List ID
var listId = userInfoList.Value.First().Id;

var userList = await graphClient.Sites[siteId].Lists[listId].Items.GetAsync((request) =>
{
    request.QueryParameters.Expand = new string[] { $"fields($select=EMail)" };
    request.QueryParameters.Top = 1000; // Default is 200, so I increased it
});

if (userList == null || userList.Value == null)
{
    Console.WriteLine($"No user records found");
    return;
}

foreach (var item in userList.Value)
{
    var listData = item.Fields?.AdditionalData;
    string userInfo = $"Lookup ID: {item.Id}";
    if(listData != null && listData.ContainsKey("EMail"))
        userInfo += $", Email: {listData?["EMail"].ToString()}";
    Console.WriteLine(userInfo);
}

Upvotes: 0

Jerry
Jerry

Reputation: 3625

The Look Id 335 is the Id in the SharePoint Hidden List named "User Information List", you can get the list guid using the request:

https://graph.microsoft.com/v1.0/sites/site-id/lists?$filter=DisplayName eq 'User Information List'

Then use the list guid in the endpoint below to get the detail email address:

https://graph.microsoft.com/v1.0/sites/site-id/lists/UserInformationListId/items/335

enter image description here

Here is a similiar thread for your reference:

How to get user from User field LookupId

Upvotes: 5

Related Questions