If you want to create multiple subsites in a site collection and the number of subsites is more than two then why create subsites from the SharePoint UI? It takes too much time, instead we can use Powershell (cmdlets) and create multiple subsites in a site collection.
  • First, we create the XML structure for creating subsites in a site collection.
  • Open Notepad++ and create one XML file for the site structure.

    input.xml
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <input>
    3. <WebApplication>
    4. <URL>http://spdev-2013:2020</URL>
    5. </WebApplication>
    6. <SiteCollection>
    7. <Name>company</Name>
    8. <Title>Company Website</Title>
    9. <TemplateCode>BLANKINTERNET#0</TemplateCode>
    10. <PrimaryAdmin>WINGTIP\SPFarmAcc</PrimaryAdmin>
    11. <SubSites>
    12. <Site>
    13. <Name>Media & Entertainment</Name>
    14. <Url>media-entertainment</Url>
    15. </Site>
    16. <Site>
    17. <Name>Travel & Hospitality</Name>
    18. <Url>travel-hospitality</Url>
    19. </Site>
    20. <Site>
    21. <Name>Online Retail</Name>
    22. <Url>online-retail</Url>
    23. </Site>
    24. <Site>
    25. <Name>Healthcare & Life Sciences</Name>
    26. <Url>healthcare-life-sciences</Url>
    27. </Site>
    28. <Site>
    29. <Name>Product Engineering Services</Name>
    30. <Url>product-engineering-services</Url>
    31. </Site>
    32. <Site>
    33. <Name>Enterprise Business Solutions</Name>
    34. <Url>enterprise-business-solution</Url>
    35. </Site>
    36. <Site>
    37. <Name>Elearning</Name>
    38. <Url>elearning</Url>
    39. </Site>
    40. <Site>
    41. <Name>Support Services</Name>
    42. <Url>support-services</Url>
    43. </Site>
    44. <Site>
    45. <Name>UX Design</Name>
    46. <Url>ux-design</Url>
    47. </Site>
    48. <Site>
    49. <Name>Documentation</Name>
    50. <Url>documentation</Url>
    51. </Site>
    52. <Site>
    53. <Name>Cloud Computing</Name>
    54. <Url>cloud-computing</Url>
    55. </Site>
    56. <Site>
    57. <Name>Business Intelligence</Name>
    58. <Url>business-intelligence</Url>
    59. </Site>
    60. <Site>
    61. <Name>Enterprise Mobility</Name>
    62. <Url>enterprise-mobility</Url>
    63. </Site>
    64. <Site>
    65. <Name>CRM</Name>
    66. <Url>crm</Url>
    67. </Site>
    68. <Site>
    69. <Name>E-commerce</Name>
    70. <Url>e-commerce</Url>
    71. </Site>
    72. <Site>
    73. <Name>Supply Chain Management</Name>
    74. <Url>scm</Url>
    75. </Site>
    76. <Site>
    77. <Name>ECM</Name>
    78. <Url>ecm</Url>
    79. </Site>
    80. <Site>
    81. <Name>Careers</Name>
    82. <Url>careers</Url>
    83. </Site>
    84. <Site>
    85. <Name>News</Name>
    86. <Url>news</Url>
    87. </Site>
    88. <Site>
    89. <Name>Global</Name>
    90. <Url>global</Url>
    91. </Site>
    92. </SubSites>
    93. </SiteCollection>
    94. <ContentDB>
    95. <Name>WSS_Content_Dummy</Name>
    96. <ServerName>SPDev-2013</ServerName>
    97. </ContentDB>
    98. </input>
  • After the XML sturucture, now we need to create a Powershell file.

  • For the Powershell file, create another file in Notepad++ and write the following Powershell script in that, save the file as whatever name you want with the extension .ps1.

    CreateSubsite.ps1
    1. Add-PSSnapin Microsoft.SharePoint.Powershell
    2. # pauses like regular cmd prompt
    3. function Pause ($Message="Press any key to continue...")
    4. {
    5. Write-Host -NoNewLine $Message
    6. $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    7. Write-Host ""
    8. }
    9. #log the operation details in text file
    10. $logfile = ".\Create_Site_Collection_Log_$(get-date -format `"yyyyMMdd_hhmmsstt`").txt"
    11. function log($string, $color)
    12. {
    13. if ($Color -eq $null) {$color = "white"}
    14. write-host "[$(Get-Date -Format T)] $string" -foregroundcolor $color
    15. "[$(Get-Date -Format T)] $string" | out-file -Filepath $logfile -append
    16. }
    17. #get the XML file
    18. [System.Xml.XmlDocument] $XmlDoc = new-object System.Xml.XmlDocument
    19. $file = resolve-path(".\input.xml")
    20. if (!$file)
    21. {
    22. log "Could not find the configuration file specified." red
    23. Break
    24. }
    25. $XmlDoc = [xml](Get-Content $file)
    26. #Get the site template code
    27. $SiteTemplate = $XmlDoc.input.SiteCollection.TemplateCode
    28. $siteCollectionUrl = $XmlDoc.input.SiteCollection.Url
    29. # clear some space
    30. Write-Host ""
    31. Write-Host ""
    32. Write-Host ""
    33. # add solution
    34. Write-Host "Checking if site:: $newSiteUrl already exist.."
    35. $targetUrl = Get-SPSite -Limit All | Where-Object {$_.Url -eq $siteCollectionUrl}
    36. if ($targetUrl -ne $null) {
    37. log "The site : $newSiteUrl is present."
    38. if($error.Count -eq 0)
    39. {
    40. log "Now Creating Subsites.."
    41. $subSites = $XmlDoc.input.SiteCollection.SubSites
    42. foreach($subsiteNode in $subSites.Site)
    43. {
    44. $SubSiteName = $subsiteNode.Name
    45. $SubSiteUrl = $newSiteUrl + "/" + $subsiteNode.Url
    46. log "Creating new subsite : $SubSiteUrl"
    47. $NewSubSite = New-SPWeb -Url $SubSiteUrl -Template $SiteTemplate -Name $SubSiteName
    48. if($error.Count -eq 0)
    49. {
    50. log "SubSite Created Successfully..!!"
    51. log "Breaking Inheritance On A Subsite"
    52. $NewSubSite.BreakRoleInheritance($true,$true)
    53. $NewSubSite.Update()
    54. }
    55. else
    56. {
    57. log "SubSite Creation failed" red
    58. log $error
    59. }
    60. }
    61. }
    62. else
    63. {
    64. log "Site Collection Creation failed" red
    65. log $error
    66. }
    67. }
    68. pause

  • Read the highlighted part in the code block of the Powershell file, $file = resolve-path(".\input.xml") , this line shows your Powershell script mapped with your XML structure.

  • After doing these two files, put both files in one folder with whatever name you want .

  • Now run the Powershell script in the SharePoint 2013 Management Shell.

  • Write the exact path of your Powershell file in the Management shell. Suppose you put your Powershell file and XML file in the C drive folder.

  • Then you write:
    C:\powershell> .\CreateSubsite.ps1