PnP CSOM SharePoint Examples

Want to learn PnP CSOM? Check the below PnP CSOM SharePoint examples. These PnP CSOM examples will help you to learn how to use PnP CSOM in SharePoint Online.

PnP CSOM Samples

Here, we will check many PnP CSOM samples or PnP CSOM examples on SharePoint sites, lists libraries, etc.

1. Create a folder in the SharePoint document library using PnP CSOM

Let us see how to create folders and subfolders inside SharePoint Document library Programmatically using PnP Core CSOM Library. We will also see if a folder exists inside a SharePoint document library programmatically using PnP.

Here, I got a requirement to create a folder and a subfolder inside a SharePoint Online document library programmatically. Here is my document library. I will create a folder and Subfolder (Inside the folder) in the “Documents” library.

create folder in sharepoint document library

The below references we have to use in the code to work with PnP libraries.

  • Microsoft.SharePoint.Client
  • OfficeDevPnP.Core

The following functions are used for creating folders in SharePoint Library.

  • FolderExists() function can be used to check folder exists or not in the SharePoint List/Document library.
  • CreateFolder() function is used to create a new folder in the Sharepoint list or document library.

Here I have created an asp.net application and I have added a button. On clicking on the button, Create a folder and subfolder in the SharePoint Documents library using the PnP core CSOM library.

The code given below creates a folder and subfolder(inside the folder) in the “Documents” Library.

AuthenticationManager authMgr = new AuthenticationManager();
string siteURL = "https://domainName.sharepoint.com/sites/TsInfoPNP";
string userName = "****@domainName.onmicrosoft.com";
string password = "*****";
protected void btnCreateFolder_Click(object sender, EventArgs e)
{
			createFolder();
}
public void createFolder()
{

	string listname = "Documents";
	string folderName = "Departments";
	try
	{
using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteURL, userName, password))
	{
	ctx.Load(ctx.Web);
	ctx.ExecuteQueryRetry();
	List list = ctx.Web.Lists.GetByTitle(listname);
	bool folderExists = list.RootFolder.FolderExists(folderName);
	if (!folderExists)
	{			 
Folder folder = list.RootFolder.CreateFolder(folderName).CreateFolder("IT");
	ctx.Load(folder);
	ctx.ExecuteQueryRetry();
	lblCreateFolder.Text = "Folder and SubFolder Created Successfully";
	}
	else
	{
	lblCreateFolder.Text = "Folder already exists";
	}
     }				
   }
catch (Exception ex) {

lblCreateFolder.Text = "Problem in creating folders in Documents library";	
			}
}

Run your application and click on the Button “Create Folder in Documents Library”.

create folder in sharepoint document library

The below snapshot shows the folder “Departments” created in the “Documents” library.

create folder in sharepoint list using pnp
create folder in sharepoint list using pnp

The below snapshot shows a sub folder created inside the folder “Departments”.

sharepoint online create folder using pnp core csom

Create a Folder and Sub Folder inside SharePoint Master Page Gallery using PnP CSOM

We can create folders and subfolders inside the SharePoint master page gallery also. You just need to change the name of the list here like below:

string listname = "Master Page Gallery";

2. Apply the master page programmatically using PnP CSOM in SharePoint

In the first PnP CSOM sample, we will see how to apply or change the master page programmatically in SharePoint Online using the PnP Core CSOM Library.

The given below references are used in the code:

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

The following functions are used in our code:

  • GetSharePointOnlineAuthenticatedContextTenant() is used for Connecting to the SharePoint online site.
  • ServerRelativeUrl() is used to get or set the server-relative URL of the website.

If you have seen the below snapshot, it is an Oslo master page of the site in SharePoint Online.

pnp csom sharepoint examples
apply a master page to a site in sharepoint

Suppose we need to change the master page from Oslo to Seattle in the SharePoint site. Manually, we can do this through the browser. But we can also do this programmatically using PnP Core CSOM in a few lines of code.

Here, I have created an asp.net application, and I have added a button. Just click on the button and apply the Master page of a site in SharePoint Online using the PnP core CSOM library.

This is a straightforward code for updating the master page of a site in SharePoint Online using PnP Core CSOM Library.


string siteURL = "https://domainName.sharepoint.com/sites/TSInfoPNP";
string userName = "******@doaminName.onmicrosoft.com";
string password = "******";
protected void btnApplyMasterPage_Click(object sender, EventArgs e)
		{
			applyMasterPage();
		}

void applyMasterPage()
{
	try
	{
using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteURL, userName, password))
     {
	Web web = ctx.Web;
	ctx.Load(web);
	ctx.ExecuteQueryRetry();
	web.MasterUrl = web.ServerRelativeUrl + "/_catalogs/masterpage/seattle.master";
	web.CustomMasterUrl = web.ServerRelativeUrl + "/_catalogs/masterpage/seattle.master";
	web.Update();
	ctx.ExecuteQueryRetry();
	lblApplyMasterPage.Text = "Applyed MastePage Successfully ";				 
     }
}
catch (Exception ex)
  {

lblApplyMasterPage.Text = "Problem in Applying MastePage in SharePoint Online";
   }
}

Run our Application and click on the button “Apply MasterPage”.

pnp csom sharepoint

See the difference between Oslo and Seattle master pages. In the Below snapshot, our Master page oslo is changed to Seattle.

pnp csom c#
apply master page sharepoint online

In this article, we learned how to change or apply the master page in the SharePoint site using the PnP Core CSOM Library. I hope you enjoyed the PnP CSOM examples or samples.

You may also like the following tutorials:

>