TeamsFx: How to use Microsoft Graph SDK v5 already

TeamsFx: How to use Microsoft Graph SDK v5 already

Looking at the latest release from the Microsoft Graph SDK team an their version 5.0.0 for dotnet three weeks ago, I was wondering how to use it with TeamsFx. I was not able to find any documentation on this topic, so I decided to write this post to share my findings.

What is the Microsoft Graph SDK v5 for dotnet?

Using the Microsoft Graph .NET SDK, you can take advantage of a fluent API and models that support retry handling, secure redirects, batching requests, large file management, and many more capabilities. This allows you to focus on what really matters in your Teams application instead of having to worry about the details of the HTTP requests and responses.

With version 5 of the SDK, Microsoft added a number of new features, including simplified method changing and a way to combine both v1.0 and beta APIs for granular early access to new features. (Source: Write simpler code with the new Microsoft Graph .NET SDK v5

What changed in v5 for TeamsFx?

There are some breaking changes including types in new namespaces/usings and also some changes around authentication. Especially the authentication changes are important for TeamsFx, as there is a new way to authenticate against the Microsoft Graph API. The current TeamsFx version, as well as the preview version are using the MsGraphAuthProvider to create a GraphServiceClient.

Let’s focus on line 3 of the current version of for a minute.

0
1
2
3
4
_logger.LogInformation("Create Microsoft Graph Client");
logger ??=_authLogger;
var authProvider = new MsGraphAuthProvider(credential,scopes, logger);
var client = new GraphServiceClient(authProvider);
return client;

This code block shows parts of the constructor of the MsGraphAuthProvider class. The constructor takes three parameters: credential, scopes and logger. The credential is the ITokenCredential object, the scopes are the scopes for the Microsoft Graph API and the logger is the ILogger object. The MsGraphAuthProvider class is using the credential and the scopes to create a token for the Microsoft Graph API. The token is then used to create the GraphServiceClient object.

The new version of the Microsoft Graph SDK v5 allows us to direclty use the TokenCredential object to create the GraphServiceClient object. This means, that we can remove the MsGraphAuthProvider class and instead use the TokenCredential object to create the GraphServiceClient when we need it.

Through dependency injection we can use the TokenCredetial from TeamsFx in our own code and don’t need to create an authentication provider for the Microsoft Graph API.

In our razor components we could reduce the call to get a new GraphServiceClient object to the following code:

0
1
2
3
4
5
private GraphServiceClient GetGraphServiceClient()
{
    //var msGraphAuthProvider = new MsGraphAuthProvider(teamsUserCredential, _scope);
    var client = new GraphServiceClient(teamsUserCredential);
    return client;
}  

That’s not a big change in terms of code in our components, but it allows us to get rid of the MsGraphAuthProvider at all. As we all know, less code is better code and fewer lines of code are easier to maintain and test.

How to update TeamsFx?

At the moment there is no official support yet for using the Microsoft Graph SDK v5 with TeamsFx. But there is always the option to clone the latest version of TeamsFx and implement the changes yourself. That’s what we did in our Microsoft Graph hack toghether sample and you can check out a working version of the modified TeamsFx version in the TeamsFxGraphSDK5 branch of our sample on GitHub. We still kept the file MSGraphAuthProvider.cs in the project, but everything in there is commented out.

Only because you can, doesn’t mean you should

An old SharePoint person

Read the quote above twice before you start using the approach of this blog post in your production code. This is just a proof of concept and not meant to be used in production. We can assume that TeamsFx will support the Microsoft Graph SDK v5 in the future, and for your work projects the smart move is to wait for the official support.