Episode #121 – Introducing Microsoft 365 PnP Core SDK (aka PnP Graph First SDK)

Here you can find the transcript of Episode #121 of PiaSys TechBites.

Welcome back to PiaSys Tech Bites. Today I want to talk with you about a topic that I really do care of, which is the PnP Core SDK, also known as the Graph first library. We all know that whenever we want to consume any of the workloads, of Microsoft 365, the Microsoft Graph is the primary rest API we should use. As PnP, we have been providing since a while ago, a library called PnP Site Core, which is based on the REST API of SharePoint and on CSOM, and helps developers to consume SharePoint on premises and SharePoint Online.

With the PnP Core SDK, we want to help developers to transition to .NET 5, and to have a seamless transition to consuming the Microsoft Graph, rather then the REST APIs of SharePoint or CSOM, and that’s why we call this library Graph first library, because our primary objective is to help developers to consume Microsoft Graph.

What is the PnP Core SDK? Is a .NET Standard library, which mainly targets SharePoint Online and Microsoft Teams, it does not target SharePoint on premises. It has been introduced as an open source project in April 2020, and it is based on huge community effort. It is the evolution of PnP Sites Core, targeting a new and modern development model, and right now it is available at the time of this recording in preview 3, but we plan to have a beta 1 for October 2020, and the V.1 or a released to market, hopefully by the end of 2020.

How does the PnP Core SDK work? First of all, as I said, it is a modern library. So it is based on .NET Standard and .NET Core. It leverages dependency, injection and services. It has an extensible architecture, and it is providing to the developers a fluent syntax, and the language integrated based in touch whenever you want to query items in SharePoint Online or Teams. Moreover, it is fully based on an asynchronous development model. The code is highly optimized, and we have a huge amount of automated tests, to guarantee an high quality of the code that we release.

The syntax from the developer point of view, is really friendly, if you are a SharePoint developer. To help SharePoint developers to easily adopt and use this new library. Let me move to the demo environment, and let me show you another word with PnP Core SDK. This is the GitHub repository where we have the source code of the PnP core SDK. You can find it under github.com/pnp/pnpcore, and from here, you can have access to the whole source code as well as to some samples. In fact, if you go into the src folder, you can find the samples folder and there, you can find a bunch of samples ready to go for us.

ASP.Net Core, Azure Function, Blazor, Console application, and so on and so forth. So first of all, let me show you the UI of an application. A really simple one, which is an ASP.NET Core Demo application that we have in the repo, which leverages the PnP core SDK, here I’m simply connected to my target tenant with the user I am right now, and I can get information about the site collection that I want to target. I can see the description, the master page of the site collection and stuff like that. I can see the list of lists that I have in this collection, and I can access one specific list to get all of the items in that list, as well as I can access, for example, a team which is backing the modern team site, that I’m targeting right now.

How can we do that with PnP Core SDK? Let me switch to visual studio and let’s see what we have, let me stop it, and first of all, this is a project where under NuGet, I referenced the NuGet package of PnP.Core.Auth. So if I go to the installed packages and I search for Pnp.Core, you can see that I have PnP.Core.Auth. This is one of the main packages included in the PnP Core SDK, and specifically the main package is PnP.Core. But if you want to leverage the out of the box authentication providers that we give you, then PnP.Core.Auth will be the package to reference, because this one will include also dependency on the main PnP.Core package. Inside this one, you will find that a set of predefined and ready to go authentication providers that you can use to authenticate against the target resources, whether it is SharePoint Online or Microsoft Teams.

Then this is a web application, so in the Startup.CS file of this ASP.NET MVC based .NET Core application, I have in the ConfigureServices method of my startup Class, simply the call to the AddPnPCore, and AddPnPCoreAuthentication to register the dependency injection for the services that we provide in PnP Core SDK.

And I configure the PnPCore library with its options, targeting a specific section of my appsettings.json file as well as I do the same for the authentication module. And I target the PnPCoreAuthenticationOptions, and I still target a PnPCore configuration section. If we go to the appsettings.json file, we can see that we have the PnPCore section, there we can define a bunch of general settings about the user agent, how to behave, while consuming SharePoint REST APIs or Microsoft Graph, how to manage the behavior of the PnP context under the covert, because we have a PnPContext object in the new Core SDK, and then I simply configure what the target site is.

And in this scenario, I simply configure the URL of my target site. Once I’ve done that in any controller of my solution, through the dependency injection, I can get the reference to the IPnPContextFactory service. And by using that service, I can simply say, okay, create a context for me, well, we seem to need to use the context factory and to say, create a specific context for a specific target site Url or eventually for a specific group by ID or team, by doing that, you will get back an object of type PnPContext, that it is a disposable object, so you will need to use a using around this object. And through that object, you can access for example, the Web or many more information about the current context. So you can access the web, the site, the team, the term store and everything else that is available right now through the SDK.

You can make a request to get for example, the title description, and the MasterPageUrl of the site. And I intentionally make a request for the MasterPageUrl, because this information is not available through Microsoft Graph. As such, the Core SDK, we’ll make a request using the REST API endpoints of SharePoint. But if you will simply target information, which are already fully available from Microsoft Graph, like for example, the title and the description, the request will go through the Microsoft Graph. So automatically the Core SDK will adapt the request in the back-end, based on the actual request you make. If you just make the requests for something which is fully available through Microsoft Graph, Microsoft Graph will be the primary choice. If you target something which is not available in Microsoft Graph, then it will be rely on the SharePoint REST API as a fallback option compared with the primary option, which is Microsoft Graph.

And as such, you can get information about the site, you can get for example, the collection of lists in the current load with the current context, and you can make a Load of some custom properties for every single list, so that I can load the ID, the title, and the description of the list, or I can make a query link on them, like I do here for example, where I say, okay, give me the list with title and title selected by the user.items. And on that collection, I make a query link and I download the ID and the title properties of every single item in the collection of items selected through this query link. I can do the same targeting a team, so I can get asynchronously set of properties, including eventually some children properties in the current object like I’m doing here.

So I get the display name, the description, and all of the channels of my current team, including the ID, and the display name for all of the channels. And then again, I can bind the output into the UI of my solution, and so on and so forth. So really simple and easy. You include the NuGet package of PnP.Core.Auth, you configure in the appsettings.json the PnPCore section. You configure through dependency injection, the services that you want to have, the PnPCore and the PnPCoreAuthentication, and then using dependency injection, you simply get a reference to the context factory service. And through that one, you create context objects, and you use them to access the target resources, simple as that. Like always thank you for watching this video. I hope you found it interesting, and I’m really looking forward to seeing you next week. And remember to subscribe to this channel. Thank you.