In SharePoint Online - What PowerShell/PnP etc. can copy a subsite to the root, and make it a site collection of its own?

frob 4,206 Reputation points
2021-05-10T23:12:46.05+00:00

Hi there

In SharePoint Online - What PowerShell/PnP etc. can copy a subsite to the root, and make it a site collection of its own?
E.g. copy siteB from /sites/siteA/siteB/ to /sites/siteB/ and make it a site collection.

Thanks.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,122 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Allen Xu_MSFT 13,756 Reputation points
    2021-05-11T02:48:11.83+00:00

    Hi @frob-0826 ,

    As there is not any API provided to copy site and save modern site as template has been removed by Microsoft, we can only extracts the site template using PnP PowerShell without its contents. If you only want the structure of the new site collection to be the same as siteB (not including the content of siteB), take a reference to the following steps.

    • Get the Source Site Schema XML
      $SiteURL = "https://xxx.sharepoint.com/sites/siteA/siteB"  
      $SchmaXMLPath = "C:\temp\SiteSchema.xml"  
      
      Connect-PnPOnline -Url $siteUrl -UseWebLogin  
      
      Get-PnPProvisioningTemplate -Out ($SchmaXMLPath) -PersistBrandingFiles -PersistPublishingFiles  
      
      This cmdlet gets all artifacts from the source site, such as content types, site columns, term store, list and libraries, theme, pages, etc. into the given template XML file. We also have a switch -includesitegroups to include site security, so that your target site will have unique permissions.
    • Create a New Site Collection $AdminCenterURL = "https://xxx-Admin.sharepoint.com"
      $SiteURL = "https://xxx.sharepoint.com/sites/siteB"
      $SiteTitle = "xxx"
      $SiteOwner = "xxx@X .onmicrosoft.com"
      $Template = "STS#3"
      $Timezone = 4 $Cred = Get-Credential Try
      {
      Connect-PnPOnline -URL $AdminCenterURL -Credential $Cred
      $Site = Get-PnPTenantSite | Where {$_.Url -eq $SiteURL}  
      
      If ($Site -eq $null)  
      {  
          New-PnPTenantSite -Url $SiteURL -Owner $SiteOwner -Title $SiteTitle -Template $Template -TimeZone $TimeZone -RemoveDeletedSite  
          write-host "Site Collection $($SiteURL) Created Successfully!" -foregroundcolor Green  
      }  
      else  
      {  
          write-host "Site $($SiteURL) exists already!" -foregroundcolor Yellow  
      }  
      
      }
      catch {
      write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
      }
    • Apply the Site Schema into Target Site with Apply-PnP ProvisioningTemplate $SiteURL = "https://xxx.sharepoint.com/sites/siteB"
      $SchmaXMLPath = "C:\Temp\SiteSchema.xml" Connect-PnPOnline -Url $SiteURL -UseWebLogin Apply-PnPProvisioningTemplate -Path $SchmaXMLPath -ClearNavigation
      If you need to copy the site with its content, I'm afraid you have to ask 3rd party migration tools for help.

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.