Working With SubSites In SharePoint 2016 Using PnP Core CSOM Library

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.

  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.     class SP2016 {  
  10.         static void Main(string[] args) {  
  11.             //Place Internal Implementation code here  
  12.         }  
  13.     }  
  14. }  
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-
    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" };Copy-paste code here to remove the line numbers.  
  • Create Client Context by passing the authentication details to the authentication manager object, given below-
    1. 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-
    1. Web subWeb = clientContext.Site.RootWeb.CreateWeb("New SubSite","NewSubWeb","Created using PnP Core library""STS#0", 1033, truetrue);  
    Output

    Output

    Full Code

    Place the code within the main function of the Console Application to get it running.
    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 = {  
    5.     "http://sharepoint2016/sites/HOL",  
    6.     "Priyaranjan",  
    7.     "password-1",  
    8.     "SharePointHOL"  
    9. };  
    10. try {  
    11.     //Create the client context  
    12.     using(var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1], authArray[2], authArray[3])) {  
    13.         //Create the web using web creation information parameters  
    14.         Web subWeb = clientContext.Site.RootWeb.CreateWeb("New SubSite""NewSubWeb""Created using PnP Core library""STS#0", 1033, truetrue);  
    15.     }  
    16. catch (Exception ex) {  
    17.     Console.WriteLine("Exception : " + ex.Message);  
    18. }  

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-
    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, as given below-
    1. var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1],authArray[2], authArray[3]) 
  • Delete the Web by calling the method ‘DeleteWeb’, as given below-
    1. //Delete the web   
    2. bool deleted = clientContext.Site.RootWeb.DeleteWeb("New SubSite");  
    3. if(deleted)  
    4. Console.WriteLine("The subsite has been succesfully deleted!");  
    Output

    Output

    Full Code

    The full code for deleting the subsite is shown below-
    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 = {  
    5.     "http://sharepoint2016/sites/HOL",  
    6.     "Priyaranjan",  
    7.     "password-1",  
    8.     "SharePointHOL"  
    9. };  
    10. try {  
    11.     //Create the client context  
    12.     using(var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1], authArray[2], authArray[3])) {  
    13.         //Delete the web   
    14.         bool deleted = clientContext.Site.RootWeb.DeleteWeb("NewSubWeb");  
    15.         if (deleted) Console.WriteLine("The subsite has been succesfully deleted!");  
    16.         Console.ReadLine();  
    17.     }  
    18. catch (Exception ex) {  
    19.     Console.WriteLine("Exception : " + ex.Message);  
    20. }  

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.
    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 retrieve the sites is shown below-
    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 = {  
    5.     "http://sharepoint2016/sites/HOL",  
    6.     "Priyaranjan",  
    7.     "password-1",  
    8.     "SharePointHOL"  
    9. };  
    10. try {  
    11.     //Create the client context  
    12.     using(var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1], authArray[2], authArray[3])) {  
    13.         //Get all web urls and iterate through them  
    14.         var urlCollection = clientContext.Site.GetAllWebUrls();  
    15.         int i = 0;  
    16.         foreach(var siteURL in urlCollection) {  
    17.             i++;  
    18.             Console.WriteLine("Site " + i + " URL : " + siteURL);  
    19.         }  
    20.         Console.ReadLine();  
    21.     }  
    22. catch (Exception ex) {  
    23.     Console.WriteLine("Exception : " + ex.Message);  
    24. }  

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.