Microsoft Graph API – How to use in Windows Forms Application

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.

Challenge-
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.

Solution –
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 –

  1. 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.
  2. Select Windows Forms App in VS for creating a new project.

  3. Let’s call solution name as ‘WindowsFormsAppWithGraph’.
  4. 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 3.0.0.0. (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.
  5. 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.
  6. 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 –

  7. Let’s register the app, so that we have the application id which will be required in code
  8. 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

  9. Let’s run the application
    1. First you will get your login prompt to Office 365

    2. When successfully logged in, you need to consent to permissions.

    3. Once done, you will see the output on page as

  10. 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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s