Episode #138 – Introducing the new PnP Framework v. 1.0.0
Here you can find the transcript of Episode #138 of PiaSys TechBites.
Welcome back to PiaSys Tech Bites. Today, I’m really excited because I’m going to introduce you to the new PnP Framework library that the PnP Community Project recently released. The PnP Framework library is an open source library. And you can find the whole source code of the library on the GitHub repo that you can see as a URL on this slide.
The PnP Framework targets .NET Standard 2.0 and .NET 5.0, and it is a full replacement of the old PnP Sites Core library that we introduced as PnP, something like five years ago or even more. Well, the PnP Sites Core library was targeting .NET Framework only, and it is nowadays retired and archived. You should use NuGet to download the new PnP Framework library in your solutions and you can also eventually use a nightly build that we will make available as a preview release of the PnP Framework library. So go and grab it and update your solutions.
Moreover, before moving to the demo environment, let me explain you the idea behind the scenes of having a PnP Framework library. We started introducing the PnP Sites Core some years ago, and that one was targeting SharePoint Online, SharePoint On-Premises, 2013/16/19. And the target Framework was the .NET Framework. Now, with PnP Framework, we provide the same capabilities, but we can use those capabilities across multiple platforms, leveraging the .NET Standard and .NET 5.0 compatibility. And we target the SharePoint Online only with this new library.
Moreover, we are also working on a new library called PnP Core SDK, which we use to call the Graph first library because, in that one, we primarily use the Microsoft Graph as the interface to manage data and settings of SharePoint Online and Microsoft Teams, and we eventually fall back to the SharePoint REST APIs for those functionalities, which are not yet available in Microsoft Graph. In the first quarter of 2021, we plan to have a GA release of the PnP Core SDK, together with the GA release of the PnP Framework that we just released few days ago. So let me move to the demo environment and let me show you how to work with the PnP Framework library.
So, let’s start having a look at the GitHub repository for the PnP Framework, which is available under github.com/pnp/pnpframework. Here you can find the whole source code of the library, as well as some documentation about how the library works. Now, let’s assume that we want to create a new project a .NET Core solution, using Visual Studio 2019, for example, and we want to create a new .NET Core Console Application. So, let me call it PnPFrameworkDemoTechBites, for example. And I will first of all, add a reference to the NuGet package of PnP Framework.
So, here is my Visual Studio, right click on Dependencies, Manage NuGet Packages, and I can search for PnP Framework. As you will see, we have version 1.0 of PnP Framework, and we can also enable the pre-release with this flag, and we can see the nightly builds that we have available right now. At the time of this recording, the 1.0 is the released version, and I will use this one, let me install it. Let me confirm all of the dependencies and let me accept all of the licenses. And the library will be referenced in my project.
So now this is a console application. I want to make it an synchronous main method and I want to have the Task type defined through Using for System.Threading.Tasks. Moreover, I want to use the PnP Framework so using PnP.Framework will be my target name space, and I also want to use the Client Side Object Model of SharePoint, as like as we are used to do with the PnP Sites Code library, if you already use that one.
So let me add using for Microsoft.SharePoint.Client, which is a dependency included in the PnP Framework. And now let me use the PnP Framework to play with CSOM, with a higher level interface like the one provided by the PnP Framework. So, I will create an authManager type, which will be a new instance of the AuthenticationManager type of PnP Framework. And I can use one of the many overloads available for this type. For example, I can use the one based on the username and password, or the one based on the client ID and device code, and so on and so forth. I will use the username and password. So I need to define a username, and in order to do that, I will simply copy and paste some ready to go code to speed up the recording of this demo.
So, that’s what I will do. I will collect the username, the password and the URL, the target site. And in order to collect the password, I will also use a method which will be this GetPassword method, which will help me to collect the password to access the target site. And then I will provide the username and the password, as a SecureString, as the input for my constructor of the AuthenticationManager. Once I have the AuthenticationManager, I can create a ClientContext of SharePoint CSOM, which will be a context variable. I will use the asynchronous method of the AuthenticationManager to get the context asynchronously for my target site. Site URL. And as such, I will have a ClientContext of a CSOM for SharePoint online. So now I can play with it. I can declare a web variable which will be context.web, I can load with the context this web object. And I can eventually include the web.Id as well as the web.Title. And I can do the context ExecuteQueryRetryAsync, so the extension method provided by the PnP Framework, which mimics exactly what we have in PnP Sites Core as well because this library is the evolution, is the successor of PnP Sites Core. And then I can eventually write some information about the site that I just loaded. So I can say the web.Id is, and the web.Title is, in the console window. Or I can play with the document library that I have in my site. So I can say web.GetListByTitle, this is an extension method that we provide in the PnP Framework. I provide the name of the library. And, for example, I want to include that the ID, as well as the title of my library.
And by doing that, I will be able to execute under the cover of this extension method the query on the context object, using CSOM to get the properties, ID, and Title of the library so that I can, later on, right on the console yet another string using the documents.Id and the documents.Title, for example. And here we are. And this could be my custom solution using CSOM and the high-level interface provided by PnP Framework, as well as the easy AuthenticationManager approach to authenticate against the target environment. Keep into account that the PnP Framework targets SharePoint Online only, as I told you.
So, if I will execute this application, control F5, of course I need to define a Using for the SecureString, my bad. So let me do it again. Control F5, and I will run my console application. Here I have a prompt for the username. So let me provide the username that I’m going to use. Let me provide the password as well, which will be a SecureString. And then let me provide the URL of the route site that I want to target. And I will get back the ID and the Title of the site, as well as the ID and the Title of the document library. And that’s it. So NuGet package for PnP Framework, you do the Using of a Microsoft SharePoint client and of PnP Framework, and you find plenty of extension methods and utility types that you can use to work with CSOM and SharePoint Online the easy way, leveraging PnP Framework.
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.