Search code examples
c#azure-devopsazure-devops-rest-api

How to get email from identityref


I've looked at the UserHttpClient, ProfileHttpClient and GraphHttpClient.

I cannot figure out how I can retrieve the email address from any of those when I have an IdentityRef from a work item (the "AssignedTo" field).

Earlier I assumed that the uniquename field always where the email address, but that seems not to be the case for premise installations?


Solution

  • I finally figured it out.

    The identityRef contains a field called Descriptor which corresponds to the "user descriptor" in the ProfileHttpClient (Rest api).

    Thus, to get the email one have to do the following:

    public static Task<string> GetEmailAddress(this VssConnection connection, SubjectDescriptor descriptor)
    {
        var client = connection.GetClient<GraphHttpClient>();
        var user = await client.GetUserAsync(descriptor.ToString());
        return user?.MailAddress;
    }
    
    // .. and in your code (where assignedTo is an IdentityRef).
    var email = await connection.GetEmailAddress(assignedTo.Descriptor);
    

    Update

    This doesn't work on Azure DevOps Server as the Graph is not available on it. So the question remains.

    (Leaves this as an answer for the cloud version)