My controller code is lke this,

public async Task<IEnumerable<CalendarEvent>> Get()
        {


            var scopes = new[] { "https://graph.microsoft.com/.default" };
            var tenantId = "xxxxx";
            var clientId = "xxxxxx";
            var clientSecret = "xxxx";
            var clientSecretCredential = new ClientSecretCredential(
                            tenantId, clientId, clientSecret);
            var graphServiceClient = new GraphServiceClient(clientSecretCredential, scopes);
            if (User == null!)
            {
                var user = await graphServiceClient.Users["xxxxx.com"].Calendar

                    .Events

                    .Request()
                    
                    
                    .Select("subject,body,bodyPreview,organizer,attendees,start,end,location")
                    .GetAsync();


                return (CalendarEvent)user;



            }

        

        }


Iam getting an error like

Unable to cast object of type 'Microsoft.Graph.CalendarEventsCollectionPage' to type 'System.Collections.Generic.IEnumerable'


I need query that sholud be given in controller.

1

There are 1 best solutions below

0
Optimal On

It's not exactly clear what you are trying to achieve but you can't convert CalendarEventsCollectionPage to IEnumerable. I am assuming that you want to return all events of specific user.

public async Task<List<Event>> GetEventsOfUser(string userId)
{
    var events = new List<Event>();

    var eventsPages = _client.Users[userId].Calendar.Events.Request()
        .Select("subject,body,bodyPreview,organizer,attendees,start,end,location");
    while (eventsPages != null)
    {
        var current = await eventsPages.GetAsync();
        events.AddRange(current.CurrentPage);

        eventsPages = current.NextPageRequest;
    }

    return events;
}

You need to fetch every page with NextPageRequest in order to get all events.