PnP stands for Practices and Patterns which 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 PnP Core CSOM Library.
PnP Core Library provides CSOM extension methods for SharePoint 2016 add-in model development. Official documentation can be accessed here. PnP Core Library increases the productivity of the developers by abstracting the 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.
In order to work with PnP Core library, we first have to install NuGet Package Manager, which is explained in this article.
Once the PnP Core Library is added, we can kick off the implementation using a Console application.
Project structure
Create a Console Application and add the references, given below-
- Microsoft.SharePoint.Client;
- OfficeDevPnP.Core;
Scope of the article will be to perform the operations, given below, using PnP Core CSOM Library-
- Creation of the sub Webs
- Deletion of the created sub Webs
- Fetch all existing sub Webs.
The code blocks for each operation will be given with a sub heading ‘Full Code’. It can be interchangeably placed within the Main function to test the working.
- 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) {
-
- }
- }
- }
Internal Implementation
Let’s explore each operation.
Create Sub site
- 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" };Copy-paste code here to remove the line numbers.
- Create Client Context by passing the authentication details to the authentication manager object, given below-
- var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1],authArray[2], authArray[3])
- Create the Web by passing the Web creation information parameters to the method ‘CreateWeb’, given below-
- Web subWeb = clientContext.Site.RootWeb.CreateWeb("New SubSite","NewSubWeb","Created using PnP Core library", "STS#0", 1033, true, true);
Output
Full Code
Place the code within the main function of the Console Application to get it running.
-
- 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])) {
-
- Web subWeb = clientContext.Site.RootWeb.CreateWeb("New SubSite", "NewSubWeb", "Created using PnP Core library", "STS#0", 1033, true, true);
- }
- } catch (Exception ex) {
- Console.WriteLine("Exception : " + ex.Message);
- }
Delete Web
Let’s see, how to apply PnP extension method to delete the Web.
- Create an instance of the authentication manager, which will be used to create the client context, as given below-
-
- 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, as given below-
- var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1],authArray[2], authArray[3])
- Delete the Web by calling the method ‘DeleteWeb’, as given below-
-
- bool deleted = clientContext.Site.RootWeb.DeleteWeb("New SubSite");
- if(deleted)
- Console.WriteLine("The subsite has been succesfully deleted!");
Output
Full Code
The full code for deleting the subsite is shown below-
-
- 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])) {
-
- bool deleted = clientContext.Site.RootWeb.DeleteWeb("NewSubWeb");
- if (deleted) Console.WriteLine("The subsite has been succesfully deleted!");
- Console.ReadLine();
- }
- } catch (Exception ex) {
- Console.WriteLine("Exception : " + ex.Message);
- }
Retrieve all sites
Now, let’s see, how to retrieve all the sites, using PnP Extension method.
- 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 retrieve the sites is shown below-
-
- 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
You can copy and paste the ‘Full Code’ fragment into the Main() of the Console Application of the project structure, given at the starting of the article to test it at your end. Thus, we have seen, how to perform SharePoint sub-site operations on remote basis, using PnP Core Library. In this method, you don’t have to get into the Server where SharePoint 2016 is installed, because it enables us to work on a remote basis.