Provision Master Pages and the other files in SharePoint Office 365 using Patterns and Practice C# CSOM


Sathish Nadarajan
SharePoint MVP
Published On :   11 May 2017
Visit Count
Today :  3    Total :   1080
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

SharePoint Office 365 Tool
Simple & Powerful Tool for Migration, Security & Reporting. Free Trial


Some time back, we saw how to provision the site columns and content types in an OLD article. But, along with that, we can upload the Master Pages, CSS, JS files to the SharePoint Site as part of provisioning. In this, the same piece of code, with the updated Provisioning XML is shown below.

 using Microsoft.SharePoint.Client;
 using OfficeDevPnP.Core.Framework.Provisioning.Connectors;
 using OfficeDevPnP.Core.Framework.Provisioning.Providers.Xml;
 
 namespace Office365.Console
 {
     class Program
     {
         static void Main(string[] args)
         {
             ProvisionMasterPagesAndJSFiles();
         }
 
         public static void ProvisionMasterPagesAndJSFiles()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https://*******.sharepoint.com/sites/communitysite";
             string userName = "Sathish@*********.onmicrosoft.com";
             string password = "************";
 
             string ResourcesDirectory = @"C:\SATHISH\PRACTICE SOURCE CODES\Office365.Console\Office365.Console\Resources";
             string ResourcesFile = "ProvisioningTemplate.xml";
 
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = clientContext.Web;
                 clientContext.Load(web);
                 clientContext.ExecuteQueryRetry();
 
                 var provisioningProvider = new XMLFileSystemTemplateProvider(ResourcesDirectory, string.Empty);
                 var provisioningTemplate = provisioningProvider.GetTemplate(ResourcesFile);
                 provisioningTemplate.Connector.Parameters[FileConnectorBase.CONNECTIONSTRING] = ResourcesDirectory;
 
                 clientContext.Web.ApplyProvisioningTemplate(provisioningTemplate);
                 clientContext.ExecuteQuery();
             }
         }
     }
 }
 

And the Provisioning Template is

 <?xml version="1.0"?>
 <pnp:ProvisioningTemplate ID="Demo.TeamSite" Version="1" xmlns:pnp="http://schemas.dev.office.com/PnP/2015/12/ProvisioningSchema">
 
    <pnp:Files>
 
     <pnp:File Src="MasterPages\My.seattle.master" Folder="_catalogs/MasterPage" Overwrite="true" />
      <pnp:File Src="JSFiles\JavaScript1.JS" Folder="SiteAssets\JS" Overwrite="true" />
   </pnp:Files>
 
 </pnp:ProvisioningTemplate>
 

The Solution will be looks like

clip_image002

Happy Coding,

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Migratiin Tools for SharePoint