7
votes

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!!

3

3 Answers

6
votes

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

1
votes

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);
     }
 }
-1
votes

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.