Wednesday, December 29, 2021

Calling the Microsoft Graph API from WinUI

 This post was originally published on my Morning Dew blog, here.


This post is part of the C# Advent Calendar 2021. Be sure to check out all the other great posts there this month!
The Microsoft Graph enables developer to query information from Microsoft 365, including data about users, mail, Teams, OneDrive, notes, and lots more. I’ve been exploring the user and teamwork APIs recently, but in this introduction, we will explore how to authenticate and retrieve some user attributes for the authenticated user account. We will create a WinUI application with the Windows App SDK, C#, .NET, and the .NET Graph SDK that will authenticate a Microsoft Account and display the name & email address for the current user. Start by installing the Visual Studio extension for the Windows App SDK to add the project templates added to your IDE. Complete instructions for getting started with the latest SDK can be found here. Next, create a new Blank App, Packaged (WinUI in Desktop) project in Visual Studio. I am using Visual Studio 2022, but 2019 is also supported: winui_graphsdk_01

Figure 1 - Creating a new WinUI project

After the project has been created, it’s always a good practice to run it to make sure things are working out of the box. Now we will add the NuGet packages needed to build our WinUI Graph app:
  • Azure.Identity – Used for interactive MSAL authentication
  • Microsoft.Extensions.DependencyInjection – Our DI container of choice for this app
  • Microsoft.Graph – The .NET Graph SDK package
  • Microsoft.Toolkit.Mvvm – A lightweight, open source MVVM framework
winui_graphsdk_02

Figure 2 – Installing the Microsoft.Toolkit.Mvvm package

Install the latest stable version of each of these packages and build the project again. We’re going to start building the app by creating a GraphService class and an IGraphService interface with one method, GetMyDetailsAsync().
public interface IGraphService
{
     Task<User> GetMyDetailsAsync();
}
The method will return a Task of Microsoft.Graph.User. We’ll get to the implementation in a moment, but first we will create an Initialize() method that will be called in the constructor. This is where the authentication code is executed.
private readonly string[] _scopes = new[] { "User.Read" };
private const string TenantId = "<your tenant id here>";
private const string ClientId = "<your client id here>";
private GraphServiceClient _client;

public GraphService()
{
     Initialize();
}

private void Initialize()
{
     var options = new InteractiveBrowserCredentialOptions
     {
         TenantId = TenantId,
         ClientId = ClientId,
         AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
         RedirectUri = new Uri("http://localhost"),
     };

    var interactiveCredential = new InteractiveBrowserCredential(options);

    _client = new GraphServiceClient(interactiveCredential, _scopes);
}
Using a TenantId in the authentication is only necessary if you want to restrict authentication to a single tenant. To read more about using Azure.Identity and MSAL, check out the docs here. The “User.Read” scope is required to access Graph data for the User object. You can read a complete list of Graph permissions here. The other aspect of using these permissions within your application is that you must have an App Registration in Azure with each required scope granted to the app in the registration. Follow these steps (under Option 2) to add your App Registration to your Azure account in the portal. This is where you will find your ClientId to enter above. Desktop applications will always use “http://localhost” as the RedirectUri. Now we’re ready to create the implementation to get our User object.
public async Task<User> GetMyDetailsAsync()
{
     try
     {
         var user = await _client.Me.Request().GetAsync();
         return user;
     }
     catch (Exception ex)
     {
         Console.WriteLine($"Error loading user details: {ex}");
         return null;
     }
}
The code here is straightforward. Using the GraphServiceClient’s fluent API, we’re requesting “Me” to be returned asynchronously. “Me” will always return a User object for the current authenticated user. Next, create a MainViewModel class, inheriting from ObservableObject in the MVVM Toolkit. This is the ViewModel for the MainWindow that was created as the main window by the WinUI project template. Use this code for MainViewModel.
public class MainViewModel : ObservableObject
{
     private string _userName;
     private string _email;

    public MainViewModel()
     {
         LoadUserDetailsCommand = new AsyncRelayCommand(LoadUserDetails);
     }

    public async Task LoadUserDetails()
     {
         var graphService = Ioc.Default.GetService<IGraphService>();
         var user = await graphService.GetMyDetailsAsync();
         UserName = user.DisplayName;
         Email = user.Mail;
     }

    public string UserName
     {
         get { return _userName; }
         set { SetProperty(ref _userName, value, nameof(UserName)); }
     }

    public string Email
     {
         get { return _email; }
         set { SetProperty(ref _email, value, nameof(Email)); }
     }

    public IAsyncRelayCommand LoadUserDetailsCommand { get; set; }
}
The ViewModel has properties to expose the UserName and Email that will be retrieved from the IGraphService call when LoadUserDetails is called and the service is fetched from the DI container. Now let’s set up the DI container in App.xaml.cs in the constructor after InitializeComponent().
Ioc.Default.ConfigureServices
         (new ServiceCollection()
             .AddSingleton<IGraphService, GraphService>()
             .AddSingleton<MainViewModel>()
             .BuildServiceProvider()
         );
This is registering GraphService and MainViewModel as singletons in the container. Any call to GetService will fetch the same instance. This works for us because we will only have one MainWindow and we only want to authenticate once during the lifetime of the application. It’s finally time to work on the UI. Start by opening MainWindow.xaml and use the following markup at the root of the Window, replacing the existing controls.
<Grid x:Name="MainGrid" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="12">
     <Grid.RowDefinitions>
         <RowDefinition Height="Auto"/>
         <RowDefinition Height="Auto"/>
         <RowDefinition Height="Auto"/>
         <RowDefinition Height="Auto"/>
         <RowDefinition Height="*"/>
     </Grid.RowDefinitions>
     
     <TextBlock Text="User name:" Margin="4" Grid.Row="0"/>
     <TextBox Text="{Binding Path=UserName}" Width="200"
              Margin="4" Grid.Row="1" IsReadOnly="True"/>
     <TextBlock Text="Email:" Margin="4" Grid.Row="2"/>
     <TextBox Text="{Binding Path=Email}" Width="200"
              Margin="4" Grid.Row="3" IsReadOnly="True"/>
     <Button Command="{Binding Path=LoadUserDetailsCommand}"
             Margin="4" Content="Load Data" HorizontalAlignment="Right" Grid.Row="4"/>
</Grid>
We’ve created a parent Grid named MainGrid with five rows, two TextBlock controls and two corresponding TextBoxes, and a Load Data Button. The two TextBox controls have their Text properties bound to the UserName and Email ViewModel properties, and the Button’s Command is bound to the LoadUserDetailsCommand, which calls LoadUserDetails(). Now let’s get the MainViewModel from the DI container in the MainWindow.xaml.cs code behind file and set it as the MainGrid.DataContext. This will complete our data binding for the window.
var viewModel = Ioc.Default.GetService<MainViewModel>();
MainGrid.DataContext = viewModel;
That’s it. You’re ready to run the app. When you click Load Data, you should be prompted to log in to a Microsoft account and accept the scopes required by the app.

winui_graphsdk_04 winui_graphsdk_05

Figure 3 & 4 – Logging in to access the Graph API

Once the login is complete, you should see your username and email address populated in the text fields. We’ve done it! And notice how the WinUI window and controls pick up your Windows theme and other UI appearance without any extra markup or code required. I’m using the dark theme in Windows 11, and my app is as well. winui_graphsdk_06

Figure 5 – Displaying the user info in our app

You can do so much more with the Graph API, using the .NET SDK or the REST APIs. Be sure to explore the docs to see what data you might want to leverage in your own apps. You can also test your API calls with the Microsoft Graph Explorer site. To learn more about WinUI, you can also check out my book from Packt Publishing, Learn WinUI 3. Get the source code for this WinUI app on GitHub to get started with WinUI and the Graph SDK on your own. Happy coding and happy holidays!

Friday, April 16, 2021

Announcing Learn WinUI 3.0 from Packt Publishing and a Contest

I'm thrilled to announce that my new book from Packt Publishing, Learn WinUI 3.0, was published in late March. You can buy your eBook or print copy today from Amazon or Packt's web site.


Windows UI (WinUI) 3 is Microsoft's next generation library for creating Windows applications with Fluent Design. WinUI 3 was released in March with Project Reunion 0.5 and is fully supported for building Win32 applications. Learn more about WinUI and Project Reunion on Microsoft Docs here.

I would like to thank Nick Randolph, who was the technical reviewer for the book. His feedback on the book's chapters and sample code were instrumental in delivering a great resource for Windows developers. I was lucky to have him involved in this project. If you aren't already following Nick's blog, go check it out now. He has been posting some great WinUI, Reunion, and Uno Platform content in recent weeks.

If you would like to win a print copy of Learn WinUI 3.0, read on. I am going to give away four copies of the book to readers of four of my blogs:

Leave a comment on this post on any of these blogs with the name of your favorite Windows or .NET development blogger. I will pick a random comment from each blog to select the four book winners. You can comment on each blog if you like, but please only comment once on each post. Comment moderation is enabled on all the blogs, so it could take a little time for your comment to appear. Make sure you sign up with a valid email address when leaving a comment. The contest closes at 11:59PM EDT on May 14th, 2021. I will ship books to winners worldwide. Please allow some time to receive the book if you are outside of the U.S.

Good luck and I hope you all enjoy my book! If you already have a copy, win one, or buy one, please leave honest ratings and reviews on Amazon. Thanks!

Sunday, January 24, 2021

My WinUI 3.0 Book Is Available for Pre-Order

Hello WinUI fans!

I have another update about my upcoming book from Packt Publishing, Learn WinUI 3.0. We are currently targeting a late March release for the book, and it is now available to pre-order on Amazon and Packt's websites. Here are the links for each:

On January 20th, the WinUI team announced three releases for Project Reunion and WinUI 3 in 2021:

  1. March 2021 - Project Reunion v0.5 and the first supported WinUI 3 release
  2. May 2021 - Project Reunion v0.8 with some WinUI 3 stable updates
  3. October 2021 - Project Reunion v1.0 with more WinUI 3 updates

Want to learn more about the current status of WinUI 3? You can watch the recording of the January Community Call on YouTube here.

If there are any changes to the publication date based on WinUI release changes, you can find out by subscribing to my Learn WinUI 3.0 newsletter here. I am currently working on the final revisions for each chapter to ensure the text and sample applications are all accurate and compatible with the supported WinUI 3 releases.

Stay tuned for more updates!

Wednesday, December 30, 2020

End-of-2020 Updates - Learn WinUI 3 and TechBash 2021

Hello Windows developers! We've nearly reached the end of 2020, and I wanted to provide a couple of updates.

First, an update on the TechBash developer conference that I help to organize. We had to cancel our 2020 event due to the pandemic. TechBash 2021 is currently planned for October 19-22, 2021 at the Kalahari Resort in the Poconos. We will be working closely with the venue to ensure that we can host a safe in-person event next fall. We are hoping that the vaccine roll-out throughout 2021 will make this possible. Watch for the call for speakers in February. Sign up for the TechBash newsletter or follow us on Twitter to stay informed.

In other news, the book I'm writing for Packt Publishing, Learn WinUI 3, has been pushed back to an early 2021 publication date. We've decide to delay it to publish closer to the delayed WinUI 3.0 launch from Microsoft. For more information about the book and its release, check out my recent newsletter. You can sign up to receive updates on the book, including when pre-orders are available.

Finally, I've refreshed the look of my primary blog, The Morning Dew. Check it out and let me know your thoughts. I have received some feedback that the link font is difficult to read on some devices, and I am working on improving that.

I hope you all stay safe and healthy during these difficult times. I hope to see many of you at events in late 2021 and 2022. Happy new year!

Alvin


Friday, September 4, 2020

An update on my book - Learn WinUI 3.0

 Summer is winding down, and I realized it's been months since I have posted anything on my WPF, UWP, and WinUI Tips blogs. I have been hard at work writing my first book, Learn WinUI 3.0 from Packt Publishing, this year. The project started in January, and I am about to start writing the first draft of the fourteenth (and final) chapter. I haven't been given the exact publication date, but it should be available about the same time that Microsoft releases Preview 3 of WinUI 3 in November, maybe a little earlier.

The November release of WinUI 3 was originally going to be Preview 4. While preview 3 isn't going to have the "go-live" moniker that Preview 4 was targeting, it is still expected to be the final preview before WinUI is released in 2021. When in 2021? Well, Microsoft isn't saying yet. However, there are two big events in the first half of 2021, another Ignite conference in early 2021 (March?) and Microsoft Build later in the first half of 2021 (June?). With November's preview likely being the final preview, I would guess that it will RTM at the Ignite event in early 2021.

While the book will be published with Preview 2 bits as the basis for the code and instruction, I will keep the book's GitHub repo updated to work with the final bits when they are available next year. We're making sure that this book will be a great reference for anyone getting started with WinUI 3. To that end, I want to give some early thanks to Nick Randolph for lending his expert Windows developer eye to the book as the technical reviewer. His feedback on the chapters and source code has been instrumental in delivering a great book for Windows developers building new WinUI apps or migrating existing Windows apps.

Want to stay in the loop with information about the book? I created a site where developers can get information about the book and sign up for a newsletter to receive updates about its release. Go check it out and let me know what additional information you would like to see there.

Sign up to learn about Learn WinUI 3.0

Stay tuned for more WinUI 3 tips and tricks here on this blog!

Sunday, February 23, 2020

WinUI 3.0 - Try the February 2020 Alpha Release

Earlier this month, at the Microsoft 365 Developer Day event, Microsoft made several important announcements for Windows developers. Among them was an alpha release of WinUI 3.0. This is the second alpha release of the upcoming major update to the WinUI libraries, due to be released toward the end of this year.

The centerpiece of this alpha release over the November 2019 alpha release is the addition of the WebView2 control. This is an updated version of the WebView control, based on the new Chromium-based Edge browser. You can read the GitHub issue with the feature proposal for WebView2 here.

WinUI 3.0 will be a major update over the 2.x releases. Outside of the WebView2, there will not be many new controls. The team is focused on building out WinUI to be a complete UI framework that can be built on top of either the UWP or Win32 platform layer for new app development. Developers can leverage C++ to build Win32 apps with WinUI or .NET to build WinUI apps with the UWP platform. It will also be possible to modernize existing WinForms and WPF apps with XAML Islands and WinUI controls. That's right, WinUI 3.0 will include XAML Islands.

Getting started with the WinUI 3.0 alpha requires Visual Studio 2019 version 16.4 or newer. You'll also need to request access to the Visual Studio extension that adds the new project templates for WinUI 3.0.

To read all of the information about the current alpha, including pre-requisites and current limitations, check out this page on MS Docs. Find out even more about the plans for 3.0 by reading the roadmap on GitHub.

Are you new to WinUI, UWP or XAML in general? Keep an eye out for my new book, Learn WinUI 3.0, coming this fall from Packt Publishing. I've just completed a draft of the first chapter, and I'm looking forward to guiding new WinUI developers in building their first Windows apps.

Happy coding!

Monday, February 10, 2020

Learn WinUI 3.0 - My New Book to be Published Fall 2020

Hello WinUI developers!

I want to update you on my WinUI focus for 2020. Most of my spare time this year is going to be spent on my new WinUI side project. I am writing a book for Packt Publishing titled Learn WinUI 3.0. The book is due out this fall. That will hopefully coincide with a final or near-final release of WinUI 3.0.


As I am working on the book, I plan to blog about my experience with WinUI 3.0 through its preview and beta stages this year. If you would like to follow along, I encourage you to subscribe to this blog. I will occasionally cross-post or create summary blog posts on my UWP Tips blog to catch up with my UWP Tips followers. Your best bet to stay informed is to subscribe to both blogs (and WPF Tips, while you're at it).


I am well underway with the first chapter and plan to blog this week about my experience setting up the WinUI 3.0 preview and creating my first project. Spoiler Alert: No issues encountered yet.


I'm looking forward to engaging the Windows developer community this year, and I expect to spend a fair amount of time harassing the WinUI team. If you have any suggestions for the book or things you would like to learn more about, don't hesitate to drop me a line on Twitter or LinkedIn.

Thanks for reading!


Alvin



Calling the Microsoft Graph API from WinUI

 This post was originally published on my Morning Dew blog, here . This post is part of the C# Advent Calendar 2021 . Be sure to check out ...