c# how to get office 365 user photo using microsoft graph api
Asked Answered
B

4

9

I want to be able to get all user's office365 photos within Azure Active directory.

Right now I'm able to get the current user's email using graph SDK

GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();

public async Task<string> GetMyEmailAddress(GraphServiceClient graphClient)
    {          
        User me = await graphClient.Me.Request().Select("mail,userPrincipalName").GetAsync();
        return me.Mail ?? me.UserPrincipalName;
    }

But I'm not sure how to integrate the the getting photos part from https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/profilephoto_get into the code.

Any help or code example is appreciated!!

Blackmore answered 9/2, 2017 at 1:24 Comment(0)
B
10

This helps to fetch the image

 GraphServiceClient graphServiceClient = GetAuthenticatedGraphServiceClient();

 Stream photo = await graphServiceClient.Me.Photo.Content.Request().GetAsync();

 if (photo != null)
 {
     MemoryStream ms = new MemoryStream();
     photo.CopyTo(ms);
     byte[] buffer = ms.ToArray();
     string result = Convert.ToBase64String(buffer);
     string imgDataURL = string.Format("data:image/png;base64,{0}", result);
     ViewBag.ImageData = imgDataURL;
 }
 else
 {
      ViewBag.ImageData = "";
 }

enter image description here

Bountiful answered 9/7, 2019 at 8:30 Comment(0)
B
4

In 2021 you can just do:

Stream photo = await graphServiceClient.Me.Photo.Content.Request().GetAsync();
            ViewData["Photo"] = Convert.ToBase64String((photo as MemoryStream).ToArray());

And then in your html:

<img src="data:image/png;base64, @ViewData["Photo"]" />
Buttonwood answered 26/10, 2021 at 20:15 Comment(0)
A
1

You get the photo using graphClient.Me.Photo.Content which will retrieve the binary data of the photo in a stream:

 public async Task GetPictureAsync()
 {
     GraphServiceClient graphClient = GetGraphServiceClient();

     var photo = await graphClient.Me.Photo.Content.Request().GetAsync();
     using (var fileStream = File.Create("C:\\temp\\photo.jpg"))
     {
         photo.Seek(0, SeekOrigin.Begin);
         photo.CopyTo(fileStream);
     }
 }
Aspirator answered 9/2, 2017 at 9:3 Comment(3)
Will I still be able to use the Microsoft Graph API if my app is registered in Azure AD endpoint? Because now I'm getting error saying that my app is not supported for this api versionBlackmore
Yes. I tested the code above with an app registration that I made in the Azure Portal, The line var photo = await graphClient.Me.Photo.Content.Request().GetAsync(); requests the v1.0 MS Graph API (graph.microsoft.com/v1.0/me/photo/$value).Aspirator
I'm getting another strange error saying Access Denied: Check Credentials and try again when I'm trying to access a user's photo using await graphClient.Users["[email protected]"].Photo.Content.Request().GetAsync(); However I can use this to get any other information regarding the user such as await graphClient.Users["[email protected]"].Request().Select("mail").GetAsync(); to get the user's emailBlackmore
T
-1

If you go to the Graph Explorer : https://developer.microsoft.com/en-us/graph/graph-explorer You can sign in with your email address. You should see a "my photo" query option. There is a documentation link which takes you here: https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/profilephoto_get You'll see a table which indicates that this only works with School or Work accounts - not with your personal Microsoft account.

Tubulure answered 18/6, 2018 at 22:57 Comment(1)
This page has some nice help too: developer.microsoft.com/en-us/graph/docs/concepts/uwpTubulure

© 2022 - 2024 — McMap. All rights reserved.