'Update user profile photo with Microsoft Graph .NET SDK

I have a problem with updating or setting a user profile picture in Microsoft graph using the Microsoft.Graph .net SDK.

Scenario:

Native app with user (user role) present including a user profile page allowing the user to update his profile.

Azure AD v2.0 endpoint used & app registration done accordingly. App is used by many tenants, therefore we have Admin Consent flow included in the apps sign-up flow.

The scopes consented are User.ReadWrite, Directory.AccessAsUser.All, Directory.ReadWrite.All, and User.ReadWrite.All.


When uploading a User profile picture using the library, we would expect the Content-Type to be image/jpeg per the documentation. Fiddler however shows content-type as application/octet-stream

PUT /v1.0/me/photo/$value HTTP/1.1
SdkVersion: Graph-dotnet-1.7.0
Cache-Control: no-store, no-cache
Authorization: bearer //removed//
Content-Length: 1051534
Content-Type: application/octet-stream
Host: graph.microsoft.com
Connection: Keep-Alive

Result is strange as I think its miss interpreted by graph

{
  "error": {
    "code": "ErrorInternalServerError",
    "message":
      "An internal server error occurred. The operation failed., The value is set to empty\r\nParameter name: smtpAddress",
    "innerError": {
      "request-id": "5b549f03-1ce5-4c8c-a393-27aef3ed1a75",
      "date": "2018-02-07T12:32:26"
    }
  }
}

The code I'm using looks like this:

if (_selectedPhoto != null)
{
    using(IRandomAccessStream raStream = await _selectedPhoto.OpenReadAsync())
    {
        await graphClient
            .Me
            .Photo
            .Content
            .Request()
            .PutAsync(raStream.AsStream());
    }
}

I addition I cloned the SDK repository and ran the Unit Test UserUpdatePhoto with the same negative result.

The corresponding method is below. Changing content type to image/jpeg does result in a 503 unknown error. Not sure what the root cause is here.

public System.Threading.Tasks.Task<Stream> PutAsync(Stream content, CancellationToken cancellationToken, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead)
{
    this.ContentType = "application/octet-stream";
    this.Method = "PUT";
    return this.SendStreamRequestAsync(content, cancellationToken, completionOption);
}


Solution 1:[1]

As per the comments, I’ve checked the Mailbox of an affected user and there was an issue with the mailbox provisioning. So, the issue was only due to a missing mailbox.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1