Duplicate Sharepoint Online Subsite with all Site Contents and Page[s] - Power Automate

 Too many was written, too scattered. So i'll try to make something complete.

Our client wans a simple flow, when SP Items is created, duplicate 2 Template Sites, BOTH ARE SUBSITES, with the Document Libraries (can be also lists), and squeeze in (inject) some content (to the pages).

Well 1st let me add that if the request would have included some design, or being a Root Site (like when you click "SharePoint" and "Create Site") than you would definitely use the "Create Site Design" ect. to some Library and apply it.

But for me its different, so lets describe the actual steps:


Its a bit of a process but I believe that you can take it in less than 1 hour with this full tutorial


Table of Content

1. Build 2 ROOT sites and design them, theme, lists, libraries, columns (no content! - for content only PNP xml Engine)

2. Create helper lists in helper site (just another side site for some management)

3. Create sites scripts, designs, and archive for later use (remember, I have 2 templates for 2 sites per record)

4. Duplicate Subsites and apply designs (design include script)

5. Copying HomePage Content


General Tip -

In PowerAutomate the "output('<name>')" or "body('<name>')" the <name> is the name of the step oyu renamed it, so when copying my code fragments please notice to set the corrent name. You can do this by adding any property from that step and hovering it with the mouse.

NOTICE - 

many of the HTTP steps have similar headers, but many are not the same and you will get some error like "Invalid files or arguments for site template '7" so dont copy-paste blindly, instead copy the right ones from each step.

Also careful from those extra spaces before and after that you dont see.



1. Build 2 ROOT sites

This is a technical step. For tutorial purpose please build 2 new root websites (click SharePoint and click "+ Create Site"), you MUST make them TEAM SITES since we can only create teams-sites as subsites. 

Also they must be root since you cant create a site scripts and design from subsite (steps in Power Automate not working)






2. Create helper lists

Some steps will need extra help by saving their states and archiving data.
I would advise to create helper site and make 3 lists:
1. Design Archive list
2. NewSubsiteAtoCopyHPcontent
3. NewSubsiteBtoCopyHPcontent

You could manage that otherwise, for me it was the simplest solution
2+3 has only Title
1 has the following, all simple text except the page content
- Title
- ScriptID
- DesignTemplateID
- ScriptName
- DesignTemplateName
- originalSiteUrl
- HomePageSaveValue




3. Create sites scripts, designs, and archive

This is a 1-time stand-alone process (for each design), and a bit long one with all the images so I made is a separate post





4. Duplicate Subsites and apply designs 

This is a simple step, where we Create Subsite and Apply Design. around it we do some waiting for the processes to complete and create a new item for the next flow.

The reason is that when I tried to make all the Page Content step here it just failed, no idea why.

In this example I made it with a manual trigger instead of "When Item is Created", and therefor created a variable instead of manually creating the SubSiteUrl according to my end needs.

In my real flow I have some rules to generate the Url/name.


The other steps are Manual Trigger, as is, 1st delay is 5 seconds, 2nd delay is 2 minutes. 
The 1st delay is recommended, the 2nd is not really important.
The variable is the full url for the new SubSite, and can be in any other place.

The Create Item is adding the new full url to a helper list A with Title only, containing the Url, for use in step for Page Content Copy


Step Create SubSite

Make sure that in Url you put the right value, which is only the last fragment for the Url for that subsite.

its a "Send an HTTP request to SharePoint" action step, and lets detail it:

a. The right Parent Site
b. POST
c.  /_api/web/webinfos/add
d. Headers
    Accept              application/json;odata=verbose
    Content-Type        application/json;odata=verbose

e. You should set the values in body all values according to your needs except WebTemplate, where "STS#3" is team site without outlook group.
Old Documentation here (couldn't find new)

{ 'parameters': 
{ '__metadata': 
{ 'type': 'SP.WebInfoCreationInformation' }, 
'Url':'ITXR-TEST-3', 
'Title':'ITXR-TEST-3', 
'Description':'ITXR-TEST', 
'Language':'1033', 
'WebTemplate':'STS#3', 
'UseUniquePermissions':false






Step Apply site Design

Since there is no way to create a Subsite with design, we must do it in 2 steps, create and apply.

Copy the Design ID from the archive list that you want for this subsite manually as it should not change usually. If you expect it to change make some ID column in your archive list and pull the right ID every time.

Its a "Send an HTTP request to SharePoint" action step, and lets detail it (no headers this time):

a. Site address is the full url of the newly created subsite
b. POST
c. Uri is
 /_api/Microsoft.SharePoint.Utilities.WebTemplateExtensions.SiteScriptUtility.AddSiteDesignTaskToCurrentWeb

d. Body  is    {  "siteDesignId": "<your design id>" }




5. Copying HomePage Content

Since it can be used generic I again make it a differen post






This has been fun and opens a lot of Ideas and automation templating options




Bonus Content







Comments

Popular posts from this blog

OverTheWire[.com] Natas Walkthrough - JUST HINT, NO SPOILERS

SOLVED The item could not be indexed successfully because the item failed in the indexing subsystem

Asp.Net Ending Response options, Response.End() vs CompleteRequest()