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.

If you are new to SharePoint PnP, following this article to know about PnP SharePoint Online Development using Patterns and Practices.

PnP CSOM Samples

Here, we will check a lot of PnP CSOM samples or PnP CSOM examples on SharePoint sites, list and libraries, etc.

1. Apply 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;

Following functions are used in our code:

  • GetSharePointOnlineAuthenticatedContextTenant() used for Connecting to the SharePoint online site.
  • ServerRelativeUrl() used to get or sets the server relative URL of the web site.

If you have seen 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 SharePoint site. Manually we can do this through the browser. But we can also do this programmatically using PnP Core CSOM in few lines of code.

Here I have created an asp.net application and I have added a button. Just click on the button, 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 button “Apply MasterPage”.

pnp csom sharepoint
apply master page sharepoint online using pnp

See the difference of oslo and seattle master page. The Below snapshot our Master page oslo is changed to Seattle.

pnp csom c#
apply master page sharepoint online

In this article, we learned about how to change or apply the master page in the SharePoint site using PnP Core CSOM Library.

You may like:

I hope you enjoyed the PnP CSOM examples or samples.

>