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.
-
- 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 authenticationManager object.
- 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
- 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.
-
- 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])) {
-
- clientContext.Web.CreateContentType("Employee CT", "0x0100A33D9AD9805788419BDAAC2CCB37509F", "Custom CT");
- Console.WriteLine("The Content Type has been created.");
- Console.ReadLine();
- }
- } catch (Exception ex) {
- Console.WriteLine("Exception : " + ex.Message);
- }
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.
-
- 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 authenticationManager object.
- var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1],authArray[2], authArray[3])
- Associate the new Content Type to List.
- 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.
-
- 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])) {
-
- clientContext.Web.AddContentTypeToListById("CT List", "0x0100A33D9AD9805788419BDAAC2CCB37509F", true, true);
- Console.WriteLine("The Content Type has been associated with the list.");
- Console.ReadLine();
- }
- } catch (Exception ex) {
- Console.WriteLine("Exception : " + ex.Message);
- }
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.
-
- 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 authenticationManager object.
- 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’.
- 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.
-
- 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])) {
-
- clientContext.Web.SetDefaultContentTypeToList("CT List", "0x0100A33D9AD9805788419BDAAC2CCB37509F");
- Console.WriteLine("The Content Type has been set as the default Content Type in the list.");
- Console.ReadLine();
- }
- } catch (Exception ex) {
- Console.WriteLine("Exception : " + ex.Message);
- }
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.
-
- 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 authenticationManager object.
- 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’.
- clientContext.Web.RemoveContentTypeFromListById("CT List", "0x0100A33D9AD9805788419BDAAC2CCB37509F");
-
-
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.
-
- 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])) {
-
- clientContext.Web.RemoveContentTypeFromListById("CT List", "0x0100A33D9AD9805788419BDAAC2CCB37509F");
-
-
- Console.WriteLine("The Content Type has been removed from the list.");
- Console.ReadLine();
- }
- } catch (Exception ex) {
- Console.WriteLine("Exception : " + ex.Message);
- }
Summary
Thus, we saw how to work with Content Types in SharePoint 2016, using PnP Core Component CSOM library.