Search code examples
c#.netsharepointmicrosoft-graph-api

Fetch peoplepicker/email from SharePoint list with .net Graph


Issue

I have a SharePoint list with a people picker.

enter image description here

I want to retrieve the email of the people picker element to send training meeting details to users. However, I can't seem to fetch the required details through .NET Graph.

Tried Resolutions

Here's what I've tried:

var test = await _graphClient
    .Sites[payload.SiteId]
    .Lists["Management"]
    .Items
    .GetAsync((requestConfiguration) =>
    {
        requestConfiguration.QueryParameters.Expand = new string[] { "fields" };
    });

This fetches all raw field data, including the people picker's lookup ID. Unfortunately, I can't resolve this lookup ID (6 in this case) to a user object.

requestConfiguration.QueryParameters.Expand = new string[] { "fields($select=skanUser)" };

Fetches the PeoplePickers primary value, which is set to display name. The other Details of the user are not included

requestConfiguration.QueryParameters.Expand = new string[] { "fields($select=skanUser/emailAddress)" };
// or
requestConfiguration.QueryParameters.Expand = new string[] { "fields($select=skanUser/email)" };
// or 
requestConfiguration.QueryParameters.Expand = new string[] { "fields($select=skanUser/EMail)" };

does not throw an error but also only returns the Fields display value (Display name)

I was unable to find any documentation.

In an unresolvable emergency case, I could change the field display value to eMail but this is undesired by the customer:

enter image description here

Any assistance, reference or documentation is highly appreciated.


Solution

  • You need to make an extra call

    There is a SharePoint list called 'User Information List' which contains details about a person from the lookup field.

    To get the id of 'User Information List' (or similar localized string)

    var result = await graphClient.Sites["{site-id}"].Lists.GetAsync((requestConfiguration) =>
    {
        requestConfiguration.QueryParameters.Filter = "displayName eq 'User Information List'";
    });
    
    var usersList = result.Value.FirstOrDefault();
    

    Now use id of usersList and lookup id

    var fields = await graphClient.Sites["{site-id}"].Lists[usersList.Id].Items["{lookup-id}"].Fields.GetAsync((requestConfiguration) =>
    {
        // comment the line below to return all fields
        requestConfiguration.QueryParameters.Select = new string []{ "email","title","username" };
    });