Search code examples
c#asp.net.netmicrosoft-graph-apimicrosoft-graph-calendar

Get calendar events from Microsoft.graph in ASP.NET Webforms


Now that I am able to get user info from 365 ( see previous question here Get user info from Microsoft.graph in ASP.NET Webforms )

I am now having problems getting calendar events.

It would appear that no matter which user I chooose, i only get 10 events? Unless I'm calling the calendar events the wrong way. I suspect that the ODataNextLink has something to do with it (see red portion)

Is there a way I can set the result page to like 100 or something?

Or better yet is there a way to ask it to give me a specific date?

I will really only ever need to grab 1 day at any given time

I suspect there is some kind of options parameters I can pass for this

Any help is appreciated

Thanks!enter image description here

ANSWER: Thanks for the help!! I found more documentation here https://learn.microsoft.com/en-us/graph/sdks/create-requests?tabs=CS

below is the correct code to get calendar events for a given user:


    var ewsScopes = new string[] { "https://graph.microsoft.com/.default" };
    var clientSecCred = new ClientSecretCredential(TenantID1, ClientID1, ClientSecret1);
    var GraphClient1 = new GraphServiceClient(clientSecCred, ewsScopes);

    var user2 = await GraphClient1.Users[UserToGet].CalendarView.GetAsync(
                requestConfiguration =>
                {
    requestConfiguration.QueryParameters.StartDateTime = 
    DateToUse.ToString("yyyy-MM-dd");
    //get the end of the day
    requestConfiguration.QueryParameters.EndDateTime = 
    DateToUse.AddHours(23).ToString("yyyy-MM-dd");
    //get the top 100 items
    requestConfiguration.QueryParameters.Top = 100;
            }
          );

   foreach (var ev1 in user2.Value)
        {

    lblDates.Text = lblDates.Text + "
" + ev1.Start.DateTime + " " + ev1.Subject; }

Solution

  • You can specify how many events will be returned by $top query parameter.

    await _client.Users["{user_id}"].Calendar.Events.GetAsync(x =>
            {
                x.QueryParameters.Top = 50;
            });
    

    If you want to get events for specific time range you can call CalendarView

    await _client.Users["{user_id}"].CalendarView.GetAsync(x =>
            {
                x.QueryParameters.Top = 50;
                x.QueryParameters.StartDateTime = DateTime.UtcNow.ToString("yyyy-MM-ddThh:mm:ss");
                x.QueryParameters.EndDateTime = DateTime.UtcNow.AddDays(7).ToString("yyyy-MM-ddThh:mm:ss");
            });
    

    CalendarView returns the occurrences, exceptions and single instances of events in a calendar view defined by a time range.

    Calendar.Events returns the list of events contains single instance meetings and series masters.