How to Create multiple SharePoint Sites using PowerShell and XML template?

Blog written by: 
Dhaval Shah
SharePoint & .Net Consultant

Download the Source Code here:https://github.com/QiPointCo/Blogs/tree/master/CreateSitesSPOnline

Install SharePoint Online SDK herehttps://www.microsoft.com/en-us/download/details.aspx?id=42038

Recently I had to develop the PowerShell script that would create indefinite number of sub sites in SharePoint Online Site Collection so that we can test some sample & dummy data for analytics of the SharePoint Sites. I took a step forward and created a PowerShell Script which would leverage the SharePoint PowerShell CSOM libraries to create hierarchy of sub sites in the Site collection along with lists/libraries and activate the required features.  Below are the functionalities that are covered in this PowerShell script

  1. Script will take the input from the configurable XML file and it can create the sub sites in the site collection that can be X level deep.
  2. Script can create the Lists and Libraries for each of the subsites
  3. Script can also activate the feature required for each of the subsites.

Script takes the input from the xml file so that It is configurable and It can also create sub sites that are X level deep.

This script works with SharePoint Online. You will need to make some modifications to the authentication used (replace Microsoft.SharePoint.Client.SharePointOnlineCredentials) for it to work with SharePoint on-premise.

Configure PowerShell Script

To execute the PowerShell script we will have to update the below variables located at the end of the script.

$siteCollectionUrl = ‘<Site Collection Url>’

$username = ‘< Admin Username>’

$password = ‘< Admin Password>’

Configuring the XML for creating the SubSites

 

PowerShell script takes the xml input of the sites hierarchy that has to be created under the site collection. Let’s say if we want to create the below Subsite hierarchy

  • Site Collection
    • Human Resources
    • Sales
      • Client 1
      • Client 2
      • Client 3
    • Projects
      • Project 1
        • Team 1
        • Team 2
      • Project 2
      • Project 3
    • Marketing
    • Accounting
    • News
    • Executive Blog
      • CEO Blog
      • Mike’s Blog
      • Shen’s Blog
    • Search
    • Reports
    • Org wiki
    • Document Center
    • Organization Policy

 

My input xml structure would look like below,

<Sites>

<Site Name=”Human Resources” Url=”Human-Resources” Template=”STS#0″ />

<Site Name=”Sales” Url=”Sales” Template=”STS#0″>

<Site Name=”Client 1″ Url=”Client-1″ Template=”STS#0″ />

<Site Name=”Client 2″ Url=”Client-2″ Template=”STS#0″ />

<Site Name=”Client 3″ Url=”Client-3″ Template=”STS#0″ />

</Site>

<Site Name=”Projects” Url=”Projects” Template=”PROJECTSITE#0″>

<Site Name=”Project 1″ Url=”Project-1″ Template=”PROJECTSITE#0″>

<Site Name=”Team 1″ Url=”Team-1″ Template=”PROJECTSITE#0″ />

<Site Name=”Team 2″ Url=”Team-2″ Template=”PROJECTSITE#0″ />

<Site Name=”Team 3″ Url=”Team-3″ Template=”PROJECTSITE#0″ />

</Site>

<Site Name=”Project 2″ Url=”Project-2″ Template=”PROJECTSITE#0″>

<Site Name=”Team 1″ Url=”Team-1″ Template=”PROJECTSITE#0″ />

<Site Name=”Team 2″ Url=”Team-2″ Template=”PROJECTSITE#0″ />

<Site Name=”Team 3″ Url=”Team-3″ Template=”PROJECTSITE#0″ />

</Site>

<Site Name=”Project 3″ Url=”Project-3″ Template=”PROJECTSITE#0″>

<Site Name=”Team 1″ Url=”Team-1″ Template=”PROJECTSITE#0″ />

<Site Name=”Team 2″ Url=”Team-2″ Template=”PROJECTSITE#0″ />

<Site Name=”Team 3″ Url=”Team-3″ Template=”PROJECTSITE#0″ />

</Site>

</Site>

<Site Name=”Marketing” Url=”Marketing” Template=”STS#0″ />

<Site Name=”Accounting” Url=”Accounting” Template=”STS#0″ />

<Site Name=”News” Url=”News” Template=”BLANKINTERNET#0″ />

<Site Name=”Executive Blog” Url=”Blog” Template=”BLOG#0″>

<Site Name=”CEO’s Blog” Url=”CEO” Template=”BLOG#0″ />

<Site Name=”Mike’s Wisdom” Url=”Mike” Template=”BLOG#0″ />

<Site Name=”Words from Shen” Url=”Shen” Template=”BLOG#0″ />

</Site>

<Site Name=”Search” Url=”Search” Template=”SRCHCEN#0″ />

<Site Name=”Reports” Url=”Reports” Template=”BICenterSite#0″ />

<Site Name=”OrgWiki” Url=”OrgWiki” Template=”WIKI#0″ />

<Site Name=”Document Center” Url=”DocCenter” Template=”BDR#0″ />

<Site Name=”Organization Policies” Url=”OrgPolicies” Template=”POLICYCTR#0″></Site>

</Sites>

 

Some of the popular templates available with SharePoint Online are,

 

No. Template Name Template Id
1 Team Site STS#0
2 Project Site PROJECTSITE#0
3 Publishing Site BLANKINTERNET#0
4 Search Site SRCHCEN#0
5 Blog Site BLOG#0
6 Business Intelligence Site BICenterSite#0
7 Wiki Site WIKI#0
8 Document Center BDR#0
9 Policy Center Site POLICYCTR#0

 

Configuring the XML to create the Lists/Libraries

 

To create the lists/libraries in the subsite the XML for the site node would look something like this,

<Site Name=”Human Resources” Url=”Human-Resources” Template=”STS#0″>

<List Name=”Shared Documents” TemplateFeatureId=”00bfea71-e717-4e80-aa17-d0c71b360101″ />

<List Name=”Policies” TemplateFeatureId=”00bfea71-e717-4e80-aa17-d0c71b360101″ />

<List Name=”Team Photos” TemplateFeatureId=”00bfea71-52d4-45b3-b544-b1c71b620109″ />

<List Name=”Quick Links” TemplateFeatureId=”00bfea71-2062-426c-90bf-714c59600103″ />

<List Name=”News &amp; Announcements” TemplateFeatureId=”00bfea71-d1ce-42de-9c63-a44004ce0104″ />

<List Name=”Team Contact” TemplateFeatureId=”00bfea71-7e6d-4186-9ba8-c047ac750105″ />

<List Name=”Team Calendar” TemplateFeatureId=”00bfea71-ec85-4903-972d-ebe475780106″ />

<List Name=”Forums &amp; Discussions” TemplateFeatureId=”00bfea71-6a49-43fa-b535-d15c05500108″ />

<List Name=”Featured Links” TemplateFeatureId=”192efa95-e50c-475e-87ab-361cede5dd7f” />

<List Name=”Team Tasks” TemplateFeatureId=”f9ce21f8-f437-4f7e-8bc6-946378c850f0″ />

<List Name=”Organization Survey” TemplateFeatureId=”00bfea71-eb8a-40b1-80c7-506be7590102″ />

</Site>

 

Above example will create the lists and libraries specified in the List node in the ‘Human Resources’ Site.

Template Feature Id attributes will take template Id of the Lists/Libraries. Below are the list of some of the OOB lists and libraries.

FeatureId Name
00bfea71-e717-4e80-aa17-d0c71b360101 Document Library
00bfea71-1e1d-4562-b56a-f05371bb0115 Form Library
00bfea71-c796-4402-9f2f-0eb9a6e71b18 Wiki Page Library
00bfea71-52d4-45b3-b544-b1c71b620109 Picture Library
00bfea71-2062-426c-90bf-714c59600103 Links
00bfea71-d1ce-42de-9c63-a44004ce0104 Announcements
00bfea71-7e6d-4186-9ba8-c047ac750105 Contacts
00bfea71-ec85-4903-972d-ebe475780106 Calendar
00bfea71-6a49-43fa-b535-d15c05500108 Discussion Board
192efa95-e50c-475e-87ab-361cede5dd7f Promoted Links
00bfea71-a83e-497e-9ba0-7a5c597d0107 Tasks (2010)
f9ce21f8-f437-4f7e-8bc6-946378c850f0 Tasks
00bfea71-513d-4ca0-96c2-6a47775c0119 Project Tasks
00bfea71-5932-4f9c-ad71-1557e5751100 Issue Tracking
00bfea71-de22-43b2-a848-c05709900100 Custom List
00bfea71-3a1d-41d3-a0ee-651d11570120 Custom List in Datasheet View
00bfea71-9549-43f8-b978-e47e54a10600 External List
00bfea71-eb8a-40b1-80c7-506be7590102 Survey
4bcccd62-dcaf-46dc-a7d4-e38277ef33f4 Asset Library
00bfea71-f381-423d-b9d1-da7a54c50110 Data Sources
00bfea71-dbd7-4f72-b8cb-da7ac0440130 Data Connection Library
a0e5a010-1329-49d4-9e09-f280cdbed37d Converted Forms
2510d73f-7109-4ccc-8a1c-314894deeb3a Report Library
00bfea71-f600-43f6-a895-40c0de7b0117 No Code Workflows
065c78be-5231-477e-a972-14177cc5b3c7 Status List
00bfea71-4ea5-48d4-a4ad-305cf7030140 Workflow History
00bfea71-f600-43f6-a895-40c0de7b0117 No Code Public Workflows
d2b9ec23-526b-42c5-87b6-852bd83e0364 Access App
ea23650b-0340-4708-b465-441a41c37af7 Persistent Storage List for MySite Published Feed
00bfea71-2d77-4a75-9fca-76516689e21a Custom Workflow Process

 

Configuring the XML to enable the features

 

Some of the lists/libraries require some feature to be activated before we can create those lists/libraries. For example, to create the publishing site, we will have to activate the publishing feature at the site collection level and site level.

We can add the feature node for each of the subsite which would activate the feature of that subsite using the feature id. XML node for the feature would something like this,

Let’s say if we want to activate the publishing feature on the human resources site our XML node will look like this,

<Site Name=”Human Resources” Url=”Human-Resources” Template=”STS#0″>

<Feature Id=”f6924d36-2fa8-4f0b-b16d-06b7250180fa” Scope=”Site”></Feature>

<Feature Id=”94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb” Scope=”Web”></Feature>

</Site>

 

Scope Attribute can be used to specify if the feature is the site collection level feature or Site level feature.

Scope = ‘Site’  -> Site collection feature

 Scope = ‘Web’  -> Site feature

 

You can find some of the sample xml files and the PowerShell script from here.

Do you use SharePoint? Try our toolkit
Download SharePoint Essentials Toolkit Now
Download the SharePoint Essentials Toolkit
Dhaval Shah - MCD, MCSD
Follow me
Latest posts by Dhaval Shah - MCD, MCSD (see all)

2 thoughts on “How to Create multiple SharePoint Sites using PowerShell and XML template?

    • Hi!
      Thanks yes hope it was useful,
      For SharePoint on-premises this will require quite a few changes but it could be done, mainly, the part where it connects

      $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$securePassword)

      should change to:

      $Creds = New-Object System.Management.Automation.PSCredential(‘test\user’,(Read-Host “Enter Password” -AsSecureString))

      (NOTE: I did not test above but something similar should work, depending on your environment.)

      Would need to be changed. This PowerShell essentially uses CSOM, which also will work with SharePoint on-premises 2013 and above.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.