SharePoint Online - Setting custom home page in Site Collection and in Sub-Sites

In SharePoint Online, using PNP’s Remote Site Provisioning approach you can request and create customized site collections.  As part of site collection creation process, one of the option could be to upload custom page and set it as default welcome page for the Site Collection.  As part of site provisioning, if you have sub-sites created as well, you may want to set the same custom page as your sub-sites welcome page. 

 

Setting  SiteCollection’s Welcome Page

 var wikiPages = web.Lists.GetByTitle("Pages");
 web.Context.Load(wikiPages);
 web.Context.ExecuteQuery();
 Microsoft.SharePoint.Client.File wikiFile = wikiPages.RootFolder.GetFile("CustomDefault-page.aspx");
 if (wikiFile.CheckOutType == CheckOutType.None)
    wikiFile.CheckOut();
  web.Context.Load(wikiFile);
 web.Context.ExecuteQuery(); 
  var wikiPage = wikiFile.ListItemAllFields;
  web.Context.Load(wikiPage);
  web.Context.ExecuteQuery();
  wikiPage["PublishingPageLayout"] = web.ServerRelativeUrl + "/_catalogs/masterpage/PageFromDocLayout.aspx, Body only";
  wikiPage.Update();
   wikiFile.CheckIn(string.Empty, CheckinType.MajorCheckIn);
  web.Context.Load(wikiFile);
  web.Context.ExecuteQuery();
   //Set Welcome Home Page
  var rootFolder = web.RootFolder;
  rootFolder.WelcomePage = "Pages/CustomDefault-page.aspx";
  rootFolder.Update();
  web.Context.ExecuteQuery();

Setting  Sub-Site’s Welcome Page

  FileCreationInformation newFile = new FileCreationInformation();
  ctx.Load(hostWeb);
  ctx.ExecuteQuery();
  Microsoft.SharePoint.Client.File file = hostWeb.GetFileByServerRelativeUrl(hostWeb.ServerRelativeUrl + "/Pages/CustomDefault-page.aspx");
  ClientResult<Stream> data = file.OpenBinaryStream();
  hostWeb.Context.Load(file);
  hostWeb.Context.ExecuteQuery();
  if (data != null)
  {
      using (var memory = new MemoryStream())
       {
          byte[] buffer = new byte[file.Length];
           int nread = 0; 
           while ((nread = data.Value.Read(buffer, 0, buffer.Length)) > 0)
          {
              memory.Write(buffer, 0, nread);
          }
            memory.Seek(0, SeekOrigin.Begin);
            StreamReader fsReader = new StreamReader(memory);
            byte[] contents = null;
            using (Stream fStream = fsReader.BaseStream)
             {
                 contents = new byte[fStream.Length];
                 fStream.Read(contents, 0, (int)fStream.Length);
                  fStream.Close();
               newFile.Content = contents;
               newFile.Url = "CustomDefault-page.aspx";
               newFile.Overwrite = true; 
               var wikiPages = newWeb.Lists.GetByTitle("Pages");
               newWeb.Context.Load(wikiPages);
               newWeb.Context.ExecuteQuery(); 
               Microsoft.SharePoint.Client.File wikiFile = wikiPages.RootFolder.Files.Add(newFile);
               newWeb.Context.Load(wikiFile);
               newWeb.Context.ExecuteQuery(); 
               wikiFile.CheckIn(string.Empty, CheckinType.MajorCheckIn);
               newWeb.Context.Load(wikiFile);
               newWeb.Context.ExecuteQuery(); 
               //Set Welcome Home Page
               var rootFolder = newWeb.RootFolder;
               rootFolder.WelcomePage = "Pages/CustomDefault-page.aspx";
               rootFolder.Update();
               newWeb.Context.ExecuteQuery();
        }
     }