简体   繁体   中英

How to retrieve the user's profile and the user's calendars using the Microsoft Graph API?

I can retrieve a user's profile using Microsoft Graph API and as well as their calendars in my web application. Sometimes, I have to get them in one request; such as the network is too slow or some reason. However, there is not a property about the calendars in the response of this API GET /me .How can I deal with this?

I use the following code using the Graph Client:

// get the user's profile
var me = await _serviceClient.Me.Request().GetAsync();

// list the user's calendars
var result = _serviceClient.Me.Calendars.Request().GetAsync();

Anyone can help me? Thank you very much.

Microsoft Graph, at its core, is an API Aggregator. In other words, it acts as a proxy for multiple product APIs (Azure Active Directory, Exchange Online, SharePoint, OneDrive, OneNote, etc.). In general, you cannot retrieve data across API boundaries.

In this case, /me is pulling data from Azure Active Directory while /calendars is pulling from Exchange Online. As such, you cannot retrieve both result sets from a single call.

You can use JSON Batching to approximate a single call but I'm not sure it will make much of a difference. The overhead of two calls vs. one is pretty minimal, and the size of the results should be nearly identical regardless. JSON Batching allows you to make a single call but it requires a slightly larger request payload (the JSON to execute) and, again, the result size should be identical to two calls). I'd expect any performance difference to be negligible at best.

To add to Marc's comments, you shouldn't assume that batching is the fastest solution because HTTP requests can be issued in parallel. It does take a bit of futzing around with Tasks but there is no reason multiple calls need to be executed sequentially.

Here's an timeline from Fiddler of a batch call doing the same thing as the two other calls.

在此处输入图片说明

According to your description, I assume you want to get the user's profile and list his profile in one request.

Refer to this document , we can using the following API to do it.

`https://graph.microsoft.com/v1.0/$batch`

And we can put the two requests in the body, like this:

`{
  "requests": [
    {
      "id": "1",
      "method": "GET",
      "url": "/me"
    },
    {
      "id": "2",
      "method": "GET",
      "url": "/me/calendars"
    }
    ]
}`

We can get the result like this (the screenshot had be truncated for brevity) 结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM