Content Type Operations In SharePoint 2016 Using PnP Core Component Library

PnP (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 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.

In order to work with PnP Core library, we first have to install the 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 below references,

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

Scope of the article will be to perform the below operations, using PnP Core CSOM Library.

  • Create Content Type with PnP Core Library
  • Associate Content Type to the list
  • Set default Content Type for the list
  • Remove Content Type from the list

Create Content Type

Let’s see how to create the Content Type with PnP extension method.

  • Create the 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 authenticationManager object.
    1. var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1],authArray[2], authArray[3])  
  • Create the Content Type. The parameters are,

    • Content Type Name
    • Content Type ID
    • Content Type Group Name
      1. clientContext.Web.CreateContentType("Employee CT""0x0100A33D9AD9805788419BDAAC2CCB37509F""Custom CT");  

Output

The new Content Type has been created.



It will be listed in the Site Content Types collection.



Full Code

The full code to create the Content Type 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 the web object from client context and create the Content Type  
  14.         clientContext.Web.CreateContentType("Employee CT""0x0100A33D9AD9805788419BDAAC2CCB37509F""Custom CT");  
  15.         Console.WriteLine("The Content Type has been created.");  
  16.         Console.ReadLine();  
  17.     }  
  18. catch (Exception ex) {  
  19.     Console.WriteLine("Exception : " + ex.Message);  
  20. }  
Associate Content Type with List

Now, let’s see how to associate the created CT with a list. 
  • Create instance of the authenticationManager 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 authenticationManager object.
    1. var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1],authArray[2], authArray[3])  
  • Associate the new Content Type to List.
    1. clientContext.Web.AddContentTypeToListById("CT List""0x0100A33D9AD9805788419BDAAC2CCB37509F",true,true);  

Output

The Content Type has now been associated with the list.



Checking the list settings will show the newly associated Content Type.



Full Code

The full code for associating the Content Type to the list is, as 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.         //Create the Content Type by specifying the List Title and Content Type ID  
  14.         clientContext.Web.AddContentTypeToListById("CT List""0x0100A33D9AD9805788419BDAAC2CCB37509F"truetrue);  
  15.         Console.WriteLine("The Content Type has been associated with the list.");  
  16.         Console.ReadLine();  
  17.     }  
  18. catch (Exception ex) {  
  19.     Console.WriteLine("Exception : " + ex.Message);  
  20. }  
Set Default Content Type for List

Now, let’s see how to set default Content Type for a list. 
  • Create instance of the authenticationManager 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 authenticationManager object.
    1. var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1],authArray[2], authArray[3])  
  • Set the default Content Type for the list using the PnP extension method ‘SetDefaultContentTypeToList’.
    1. clientContext.Web.SetDefaultContentTypeToList("CT List""0x0100A33D9AD9805788419BDAAC2CCB37509F");  

Output

The newly created Content Type has been set as the default Content Type in the list.



The default CT can be seen from the list settings.



Full Code

The full code to set the Content Type as the default list Content Type is, as 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.         //Create the Content Type by specifying the List Title and Content Type ID  
  14.         clientContext.Web.SetDefaultContentTypeToList("CT List""0x0100A33D9AD9805788419BDAAC2CCB37509F");  
  15.         Console.WriteLine("The Content Type has been set as the default Content Type in the list.");  
  16.         Console.ReadLine();  
  17.     }  
  18. catch (Exception ex) {  
  19.     Console.WriteLine("Exception : " + ex.Message);  
  20. }  
Remove Content Type

Now, let’s see how to remove the created Content Type from the list, 
  • Create 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 authenticationManager object.
    1. var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1],authArray[2], authArray[3])  
  • Remove the Content Type from the list using the PnP extension method ‘RemoveContentTypeFromListByName’ or ‘RemoveContentTypeFromListById’.
    1. clientContext.Web.RemoveContentTypeFromListById("CT List""0x0100A33D9AD9805788419BDAAC2CCB37509F");  
    2. // We can also use the below method to remove CT by Name - List Title and CT Name are the parameters  
    3. //clientContext.Web.RemoveContentTypeFromListByName("CT List", "Employee");  

Output

The Content Type has now been removed from the list.



Full Code

The full code to remove the Content Type from the list is, as 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.         //Remove the Content Type by specifying the List Title and Content Type ID  
  14.         clientContext.Web.RemoveContentTypeFromListById("CT List""0x0100A33D9AD9805788419BDAAC2CCB37509F");  
  15.         // We can also use the below method to remove CT by Name - List Title and CT Name are the parameters  
  16.         //clientContext.Web.RemoveContentTypeFromListByName("CT List", "Employee");  
  17.         Console.WriteLine("The Content Type has been removed from the list.");  
  18.         Console.ReadLine();  
  19.     }  
  20. catch (Exception ex) {  
  21.     Console.WriteLine("Exception : " + ex.Message);  
  22. }  
Summary

Thus, we saw how to work with Content Types in SharePoint 2016, using PnP Core Component CSOM library.