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’.
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.
Now, let’s go ahead and install NuGet package ‘SharePointPnP2016’ by running the command, give below:
Install-Package SharePointPnPCore2016
This will add the dependencies and the packages.
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.
-
- OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();
-
- string[] authArray = { "http://sharepoint2016/sites/HOL", "Priyaranjan","password-1","SharePointHOL" };
- Create Client Context by passing the authentication details to the authentication manager object.
- var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1],authArray[2], authArray[3])
- Get the Web URL collection and iterate through the collection.
- var urlCollection = clientContext.Site.GetAllWebUrls();
Output
Full Code
The full code to connect to SharePoint and retrieve the Web collection, using PnP Core Component is shown below:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint.Client;
- using OfficeDevPnP.Core;
- namespace OfficeDevPnP
- {
- class SP2016
- {
- static void Main(string[] args) {
-
- OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();
-
- string[] authArray = {
- "http://sharepoint2016/sites/HOL",
- "Priyaranjan",
- "password-1",
- "SharePointHOL"
- };
- try {
-
- using(var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1], authArray[2], authArray[3])) {
-
- var urlCollection = clientContext.Site.GetAllWebUrls();
- int i = 0;
- foreach(var siteURL in urlCollection) {
- i++;
- Console.WriteLine("Site " + i + " URL : " + siteURL);
- }
- Console.ReadLine();
- }
- } catch (Exception ex) {
- Console.WriteLine("Exception : " + ex.Message);
- }
- }
- }
- }
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.