Creating a site with more than just the Home page

Posted by

I’ve been considering the best way to programmatically create new sites in SharePoint. Ideally, it should be possible to create some templates for the end users to utilise, and these would apply a common look and feel, and perhaps create a few lists and pages to help them get going.

The natural starting point for this would be to create a site script and associated site design, and it’s great for adding themes, site logos and simple lists but pretty soon things can get complicated. And as the title of this post infers, there is no way a creating new pages. However, it is possible to use the site script to trigger a Flow, and then that can be used to do far, far more, such as calling a PnP provisioning engine script.

Getting a template

The nice thing about using the PnP provisioning engine is that you don’t have to create the template from scratch. You can create a new sites, and configure the lists, content types, pages etc., and then export the xml template. This creates a neatly formatted file, and it’s fairly easy to read. However, there are a couple of shortcomings:

  1. Only the Home page is exported
  2. None of the images or other files are exported

PnP Provisioning Schema

Looking at the current PnP Provisioning schema there are a couple of interesting sections:

The schema implies that you can create more than one page:

Similarly for the files it implies you can add them:

Getting all the pages

It is possible to get all of the pages for  a site using a bit of cunning.  João Ferreira has written a script which cycles through all of the pages in a site, sets them as the home page and then exports the template for that page. At the end all the templates are then combined into a single file. It works pretty well, and when the template is used, the pages are created and the individual web part configuration is recreated as expected. It does fail occasionally with oddly named pages, but it is easy enough to resolved these and manually create the template.

Adding the files

Part of the provisioning process means that you would need to upload some files; these could be document templates or guidelines stored in the Documents library, or they could be the images used for the page headers. The schema is pretty simple for the files, but then things start to go rapidly wrong.

The Src attribute only takes local values. I want this site to be provisioned remotely using the guidance provided by Microsoft; I guess I could upload the images to the Azure Function alongside the template XML file, but that doesn’t feel like the right place to store these files, and future updates could be difficult.

Hosting the files elsewhere

If I’m going to put the files somewhere else, I’d like them to be kept either in a public CDN or in the root site of the SharePoint tenant (e.g. https://tenant.sharepoint.com). Looking again at the xml for the pages, there’s a relative reference for the page header:

The ServerRelativeImageUrl is a path to the image file; the exported template mirrors the directory in which the file was originally stored when it was uploaded. The {site} token here is a core provisioning token and looking at the list they are few that are useful in this case. The {site} and {sitecollection} both point back to the current site, and as I have so far failed to automatically copy files there, they are not useful. However the {hosturl} looks perfect. A quick rework of the file:

And failure…

Repeating the process using the fully URL or removing the {hosturl} token produce the same results.

Flow

My intention is to provisions sites using a site script to trigger a Flow. This can then be used to process the PnP provision script. I’m not limited to just running the provisioning script. I could also use Flow to create files into the new site. As far as I can tell the script doesn’t care if image URL can be resolved, so the order in which I provision the site and copy the files shouldn’t matter. Creating the files using Flow seems well documented so I don’t think there will be any issues.

Leave a Reply

Your email address will not be published. Required fields are marked *