Hackathon - Hack Together: Microsoft Graph and .NET 🦒 - Day 04
Tags: azure, csharp, webassembly, dotnet
Published: 2023-03-07 18:06:10

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:

```json
{
  "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"
}
```

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

```csharp
        // 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;
```

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.

```csharp
    var mail = await graphClient.Me.Messages.Request().OrderBy("receivedDateTime desc").GetAsync();
     var todos = await graphClient.Me.Todo.Lists.Request().GetAsync();
```

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](https://bit.ly/learninggithub).

![LinkedIn Learning](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdc2bpiftpadibi4h51c.gif)

---

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

Feel free to follow me to get notified when new articles are out 🙂

{% embed https://dev.to/kasuken %}