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

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”.

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

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:
- Create SharePoint Online Modern Page + PnP PowerShell
- SharePoint Online PowerShell: Disable versioning in a document library (PnP PowerShell)
- Remove all items from a SharePoint Online list using PnP PowerShell
- PnP PowerShell SharePoint Online – How to Connect
- PnP PowerShell Commands for SharePoint Online List
- PnP PowerShell commands for SharePoint Online Site
I hope you enjoyed the PnP CSOM examples or samples.
I am Bijay a Microsoft MVP (8 times –Â My MVP Profile) in SharePoint and have more than 15 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com