问题
I'm trying to develop a console app to list, remove and add events to a specific user's calendar. I'm developing a business process. I'm starting by listing events.
Calendar cal =
client
.Users["UserName@CompanyName.com"]
.Calendars["AAMkAGIzZDM4O...."]
.Request()
.GetAsync()
.Result;
I get the correct calendar back but the Events collection is null. There are 13 events in the calendar. I have Calendar Read/Write permissions.
Any ideas?
回答1:
I don't have an environment to test the c# Graph SDK, but I suspect the underlying query doesn't ask Graph for the calendar events so that's why the field is null.
Using Graph explorer (https://developer.microsoft.com/en-us/graph/graph-explorer#), we can try an equivalent query and note only metadata about the calendars are returned.
GET https://graph.microsoft.com/v1.0/me/calendars
The Graph explorer has some calendar sample queries. They might not be visible by default, so click the 'show more samples' link.
The all events in my calendar
sample query is probably what you're looking for. The request is:
GET https://graph.microsoft.com/v1.0/me/events?$select=subject,body,bodyPreview,organizer,attendees,start,end,location
(or /calendars/{calendar-id}/events if you want events for a specific calendar)
The next step is converting this REST API query into the SDK syntax to use in your application.
From this sample, a starting point is:
IUserEventsCollectionPage events = await graphClient.Me.Events.Request().GetAsync();
Besides querying the events, you might also want to look into querying the users calendar view which allows you to specify a start and end date.
回答2:
Dan, thanks for the post. I realized my mistake after reading your post. Here is the code that works:
string userEmail = "UserName@CompanyName.com";
string calId = "AAMkAGIzZDM4OWI0LWN...….";
ICalendarEventsCollectionPage events = client.Users[$"{userEmail}"]
.Calendars[$"{calId}"]
.Events.Request().GetAsync().Result;
回答3:
It's worth noting here that Calendars["..."].Events
will only give you event masters.
An alternative is to use Calendars["..."].CalendarView
with query options of "startDateTime" and "endDateTime" which will give you all the event instances in that time frame.
来源:https://stackoverflow.com/questions/52693341/getting-events-from-calendar-using-c-sharp-and-microsoft-graph-sdk