Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Microsoft Graph Beta API from C#

I tried to get the content of the user's profile picture and I found out that I had to call the Beta version because the current version gives the following error message:

"code": "GetUserPhoto",
"message": "The operation is not supported."

So, I tried to switch to Beta, and here is the code that I wrote in C# to do it, but it doesn't work:

Microsoft.Graph 1.6.2

List<QueryOption> options = new List<QueryOption>
{
    new QueryOption("$api-version", "beta")
};

var pictureStream = await graphClient.Me.Photo.Content.Request(options).GetAsync();

I got the same error message.

I tried the same request in the Graph Explorer. The 1.0 doesn't work, but Beta works.

like image 739
daniel simionescu Avatar asked Nov 07 '17 15:11

daniel simionescu


People also ask

How do you use Microsoft Graph Beta?

The Microsoft Graph SDK for ObjC requires you to build a URL string to the API you want to call. It provides a constant MSGraphBaseURL for the v1. 0 endpoint. To use beta, you simply replace that with https://graph.microsoft.com/beta .


2 Answers

The api-version query parameter is used by the Azure AD Graph API. This is a different API than Microsoft Graph. There is a lot of functional overlap (Azure AD Graph is slowly being migrated over to Microsoft Graph) but they use entirely different entities and calling conventions.

In order to call the /beta endpoint using the Microsoft Graph .NET Client Library, you need to change the BaseUrl of the client:

graphClient.BaseUrl = "https://graph.microsoft.com/beta";
var pictureStream = await graphClient.Me.Photo.Content.Request().GetAsync();

Some important notes about the /beta endpoint:

  1. It isn't supported and isn't suitable for production. So don't do that. Or at least don't tell anyone and don't call Support if it stops working. ;-)

  2. The .NET Client uses objects constructed off the production metadata. This means that any entities, actions or properties that were added in /beta don't exist in the models shipped with the SDK.

  3. The .NET Client will ignore any values returned by Microsoft Graph that it doesn't expect to see. So if an endpoint returns a property that wasn't included in the production metadata (see #2), it will simply be ignored.

    So long as you're only using a /beta to gain functionality but still expecting /v1.0 results, it should work okay. Photos for example only look at Exchange in v1.0 but look in both Exchange and Active Directory but still return the same result. In theory this means you should be able to swap /beta for /v1.0 without a problem.

like image 162
Marc LaFleur Avatar answered Oct 02 '22 14:10

Marc LaFleur


I think you are still calling V1 endpoint. In fact, the Beta endpoint is not currently supported in the Microsoft Graph .NET Client Library. More info here.

like image 20
FIL Avatar answered Oct 02 '22 15:10

FIL