.netcalendarmicrosoft-graph-apioutlook-calendar

Outlook calendar Syncing issue


I am using Microsoft graph library for syncing the events but its returning always top 10 events but my requirement are below, 1- Get the count of events between date range. 2-Get all events between date range.

Code are,

 GraphServiceClient client = new GraphServiceClient(
                new DelegateAuthenticationProvider(
                    (requestMessage) =>
                    {
                        requestMessage.Headers.Authorization =
                            new AuthenticationHeaderValue("Bearer", accessToken);
                        if (!string.IsNullOrEmpty(userEmail))
                        {
                            requestMessage.Headers.Add("X-AnchorMailbox", userEmail);

                        }
                        return Task.FromResult(0);
                    }));


var eventResults = client.Me.Events.Request()
                        .GetAsync().Result;

Solution

  • If you want to get the calendar events in a specific range you have to use calendarView (see documentation). Using graph SDK it could look like this.

            List<Event> eventList = new List<Event>();
            List<QueryOption> options = new List<QueryOption>()
                            {
                                new QueryOption("startDateTime",startDateTime),
                                new QueryOption("endDateTime",endDateTime),
                                //new QueryOption("$top",top),
                                //new QueryOption("$select",select)
                            };
    
            var request = graphClient.Users[userId].Calendar.CalendarView.Request(options);
    
            var result = await request.GetAsync();
    
            if (result != null)
            {
                eventList.AddRange(result);
    
                var nextPage = result;
                while (nextPage.NextPageRequest != null)
                {
                    var nextPageRequest = nextPage.NextPageRequest;
                    nextPage = await nextPageRequest.GetAsync();
                    if (nextPage != null)
                    {
                        eventList.AddRange(nextPage);
                    }
                }
            }
    

    Get the event count with eventList.Count.