Set Up PnP Core Component CSOM Library To Work With SharePoint 2016

PnP, which stands for Practices and Patterns, is a community driven open source project, where Microsoft and community members have created an implementation pattern for Office 365 and SharePoint on-premises. One of the branches of the PnP development is in PnP Core CSOM Library.

PnP Core library provides CSOM extension methods for SharePoint 2016 add-in model development. The official documentation can be accessed from here. PnP Core library increases the productivity of the developers by abstracting complex operations. In this article, we will see, how to set up PnP core library remotely and work with SharePoint 2016, using a console Application.

As the first step, we have to install SharePoint PnP Core library for SharePoint 2016. In order to do this, we will head to Visual Studio and from Tools menu, select ‘NuGet Package Manager’ and spin up ‘Package Manager Console’.

Console

In order to see if PnP core library is available, run the script, given below, in the Package Manager Console.

Get-Package -Filter SharePointPnPCore2016 -ListAvailable

This will list out the details of the PnP Core component.

details

Now, let’s go ahead and install NuGet package ‘SharePointPnP2016’ by running the command, give below:

Install-Package SharePointPnPCore2016

command

This will add the dependencies and the packages.

command

Finally, we got the Installation success message.

Now, let’s try to program against SharePoint 2016. Before that, we have to add the references, given below, to the Console Application. ‘Microsoft.SharePoint.Client’ is the CSOM library, which helps to remotely access SharePoint. ‘OfficeDevPnPCore’ is the PnP core component, which facilitates the extension methods that simplifies SharePoint operations.

  • Microsoft.SharePoint.Client;
  • OfficeDevPnP.Core;

Internal Implementation

We will try to connect to SharePoint, using the CSOM and PnP Library from within the Console Application. Once connected, we will retrieve the SharePoint Web Collection. Let’s see, how it’s internally implemented.

  • Create an instance of the authentication manager, which will be used to create the client context.
    1. //Get instance of Authentication Manager  
    2. OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();  
    3. //Create authentication array for Site url,User Name,Password and Domain  
    4. string[] authArray = { "http://sharepoint2016/sites/HOL""Priyaranjan","password-1","SharePointHOL" };  
  • Create Client Context by passing the authentication details to the authentication manager object.
    1. var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1],authArray[2], authArray[3])  
  • Get the Web URL collection and iterate through the collection.
    1. var urlCollection = clientContext.Site.GetAllWebUrls();  

Output

Output

Full Code

The full code to connect to SharePoint and retrieve the Web collection, using PnP Core Component is shown below:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint.Client;  
  7. using OfficeDevPnP.Core;  
  8. namespace OfficeDevPnP   
  9. {  
  10.     class SP2016   
  11.     {  
  12.         static void Main(string[] args) {  
  13.             //Get instance of Authentication Manager  
  14.             OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();  
  15.             //Create authentication array for site url,User Name,Password and Domain  
  16.             string[] authArray = {  
  17.                 "http://sharepoint2016/sites/HOL",  
  18.                 "Priyaranjan",  
  19.                 "password-1",  
  20.                 "SharePointHOL"  
  21.             };  
  22.             try {  
  23.                 //Create the client context  
  24.                 using(var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1], authArray[2], authArray[3])) {  
  25.                     //Get all web urls and iterate through them  
  26.                     var urlCollection = clientContext.Site.GetAllWebUrls();  
  27.                     int i = 0;  
  28.                     foreach(var siteURL in urlCollection) {  
  29.                         i++;  
  30.                         Console.WriteLine("Site " + i + " URL : " + siteURL);  
  31.                     }  
  32.                     Console.ReadLine();  
  33.                 }  
  34.             } catch (Exception ex) {  
  35.                 Console.WriteLine("Exception : " + ex.Message);  
  36.             }  
  37.         }  
  38.     }  
  39. }  
Summary

Thus, we have seen, how to set up PnP Core component CSOM library and work with SharePoint 2016 remotely from a console Application. We will see more of PnP Core component in action in the upcoming articles.