DEV Community

Cover image for Hackathon - Hack Together: Microsoft Graph and .NET ๐Ÿฆ’ - Day 04
Emanuele Bartolesi
Emanuele Bartolesi

Posted on

Hackathon - Hack Together: Microsoft Graph and .NET ๐Ÿฆ’ - Day 04

Today I fixed the issue that I had yesterday about retrieving data from a CalendarView from the userโ€™s calendar.
I asked for help to two friends of mine (Patrick and Federico) because I wasted 3 hours on the same issue.
After a quick brainstorming (via chat) I figured out what is the problem.
Maybe during my tests I created an App Registration and I consented a permission called โ€œCalendar.ReadBasicโ€ and then I tried to call the Graph SDK with another permission in the scope as โ€œCalendars.ReadWriteโ€.
In my token I had both scope and of course the less security scope win and I obtained โ€œAccess Deniedโ€.
I deleted my App Registration and I created a new one with the right permission and it started to work well. ๐Ÿ˜Š

Lesson learned.

What I have now in my appsettings.json file:

{
  "AzureAd": {
    "Authority": "https://login.microsoftonline.com/common",
    "ClientId": "5c1b3387-b2ca-4513-83e7-3c802665bea0",
    "ValidateAuthority": true
  },
  "GraphScopes": "Files.Read;Tasks.ReadWrite;User.Read;MailboxSettings.Read;email;offline_access;openid;profile;Mail.Read;Calendars.ReadWrite"
}
Enter fullscreen mode Exit fullscreen mode

And now to retrieve the data from the calendar I wrote the following code:

        // Get the user
        var user = (await authenticationStateTask).User;
        var graphTimeZone = user.GetUserGraphTimeZone();
        dateTimeFormat = $"{user.GetUserGraphDateFormat()} {user.GetUserGraphTimeFormat()}";

        // Calculate the start and end of the current week in user's time zone
        var startOfWeek = GetUtcStartOfWeekInTimeZone(DateTime.Today, graphTimeZone);
        var endOfWeek = startOfWeek.AddDays(7);

        graphClient = clientFactory.GetAuthenticatedClient();

        // Specifies the start and end of the view on the calendar
        // Translates to: ?startDateTime=""&endDateTime=""
        var viewOptions = new List<QueryOption>
        {
            new QueryOption("startDateTime", startOfWeek.ToString("o")),
            new QueryOption("endDateTime", endOfWeek.ToString("o"))
        };

        var eventPage = await graphClient.Me
            .CalendarView
            .Request(viewOptions)
            .Header("Prefer", $"outlook.timezone=\"{graphTimeZone}\"")
            .Top(50)
            .Select(e => new
            {
                e.Subject,
                e.Organizer,
                e.Start,
                e.End
            })
            .OrderBy("start/dateTime")
            .GetAsync();

        events = eventPage.CurrentPage;
Enter fullscreen mode Exit fullscreen mode

As you can see at the beginning I try to retrieve some information from the user profile like the TimeZone and the date format.

Then I retrieve the items for the current week and I collect them in a List.
Now I am ready to display all the items in our homepage.

I think tomorrow I will create at least four components to display all the data I have now: Calendar items, Mails and Todos.

    var mail = await graphClient.Me.Messages.Request().OrderBy("receivedDateTime desc").GetAsync();
     var todos = await graphClient.Me.Todo.Lists.Request().GetAsync();
Enter fullscreen mode Exit fullscreen mode

In the code above you can see how I retrieve the emails (ordered by received date and time descending).
I have to work on the Todos items because at the beginning I need to retrieve all the lists (as in the code above) and then from each lists I need to retrieve the items that expired today.

I think I am on track with all the activities for the due date of the Hackathon.

Stay tuned.


Are you interested in learning GitHub but don't know where to start? Try my course on LinkedIn Learning: Learning GitHub.

LinkedIn Learning


Thanks for reading this post, I hope you found it interesting!

Feel free to follow me to get notified when new articles are out ๐Ÿ™‚

Top comments (0)