Microsoft Graph API is not new for developers in the world of Microsoft 365 (Office 365). Please check my previous blog about the summary of Graph API services summary.
As this is an API, you can consume in your supported application in number of ways, either use SDK or use the REST end point of the service.
The latest type of applications where Graph API is used are Office Add-ins, SharePoint Framework components, Microsoft Teams extensions or Office 365 Apps.
Consider if you have a business critical application which is built in Windows Forms or a WPF. That sounds like stone age in current technology world, but there are businesses which are still running on those legacy platforms. The challenge in front of those businesses is that they want to use latest technology without leaving the comfort zone.
With new Windows Community Toolkit, a GraphLogin component is being made available to be used in windows form application. This provides an easy to use experience for authenticating with Azure AD and Microsoft Graph.
What is this components – This component wraps the Toolkit’s MicrosoftGraphService for an easy to use login experience. The control then provides read-only properties about the logged on user and instance of GraphServiceClient which can be used for additional call with Microsoft Graph SDK.
Let’s build it –
- First make sure that you have .NET Framework equal or higher than 4.6.2. If not, please update the framework based on your VS version.
Select Windows Forms App in VS for creating a new project.
- Let’s call solution name as ‘WindowsFormsAppWithGraph’.
Now, next installing the Nuget packages, so that we can get required components in the solution.
- First add, ‘Microsoft.Toolkit.services’. Current version as of publishing was 22.214.171.124. (I got error that it cannot resolve dependency on Microsoft.Identity.Client nuget package. In that case, I installed it first and then there was no issue).
- Then install, ‘Microsoft.Toolkit.Win32.UI.Controls’ nuget package.
After successfully installing Nuget packages, let’s add the GraphLoginComponent control in the toolbox pane.
- Right click in toolbox –> Choose items –> Browse. –> \packages\Microsoft.Toolkit.Services.3.0.0\lib\net461\Microsoft.Toolkit.Services.dll –> Click Ok –> Click Ok.
- Now, you should see GraphLoginComponent in toolbox.
Let’s add following controls on the form to design the information. Keep all properties as it is else defined below
- 2 Labels
- 1 Button
- 1 picture box [set properties – StretchImage – StretchImage]
- 1 datagridview
- 1 GraphLoginComponent
Form will look like below –
- Let’s register the app, so that we have the application id which will be required in code
Let’s add functionality now. Highlighted with yellow boxes in below screen shot –
Look at the code available at https://github.com/ashuetawah/WindowsFormsAppWithGraph/blob/master/WindowsFormsAppWithGraph/Form1.cs
Let’s run the application
First you will get your login prompt to Office 365
When successfully logged in, you need to consent to permissions.
Once done, you will see the output on page as
- In the code, Microsoft.Graph.GraphServiceClient graphClient = graphLoginComponent1.GraphServiceClient; will help you to get other APIs calls straight using GraphClient object.
Please let me know your comments. I will be sharing WPF code with Graph API soon.