Monday 12 January 2015

Clone one SharePoint site collection and create a new one : SharePoint


One of my colleague asked me that he wants to create couple of different site collection by using existing site collection on production server. And he is facing errors with export/import and backup/restore process.

I promised him that I will provide him some solution :). I also done some googling, I found some help from vlad blog (I am following this guy since 1 year and you can visit his blog on SharePoint Community site. He is genius!!! )

1- Create Site Template using PowerShell

$Web=Get-SPWeb <Existing Site URL whose template needs to be created>
$Web.SaveAsTemplate("templateName","Template Title","Description",0)

- Use 0 without data else 1 with Data

Note:- Sometimes, you face following error.









To resolve this issue use following cmdLet

$site = Get-SPSite -Identity "<Site Collection whose template needs to be created>"
$web = $site.RootWeb;
$web.SaveSiteAsTemplateEnabled = $true;
$web.Update();

2- Now go in above site and open Solution link available under Web Designer Galleries (Under Site Settings)
   Click on added template.wsp and save it on machine where you want to create new site collection with same template

3- Create new Site Collection with blank tempalte (Without any template selection)
   $mainurl = "<URl of new site collection going to be created now>"
   Write-Host -ForegroundColor White " – Creating Site Collection  $mainurl…" -NoNewline
   New-SPSite $mainurl -OwnerAlias Domain\User  -Name "<Any Name you think>"
   Write-Host -ForegroundColor GREEN "Done."
   Write-Host

4- Add and Install Site Template as we created in 1 section and saved on machine.
   Write-Host -ForegroundColor White " – Adding and Installing Your Custom Template…" –NoNewline]
              Add-SPUserSolution -LiteralPath "<path of wsp>\<Name of WSp file>.wsp" -Site $mainurl
             $ErrorActionPreference = "silentlycontinue"
                do
                {
Write-Host "." -NoNewline -ForeGroundColor White;
                                Start-Sleep -Seconds 5;                              
                                try
                                {
                                $testsolution = Get-SPUserSolution -Identity <Name of WSp file>.wsp -Site $mainurl
                                }
                                catch
                                {}
                } while(!$testsolution);
          $ErrorActionPreference = "stop"
          Install-SPUserSolution -Identity <Name of WSp file>.wsp -Site $mainurl
          Write-Host -ForegroundColor GREEN "Done."
           Write-Host

5- get the template name
   $template =  $web.GetAvailableWebTemplates(1033) |?{$_.Title -eq '<Name of WSp file>'} | select Name
   $template (Result will be like {2752F8B3-5B44-4358-95A9-3D1DBFE55DE4}#AltoSite)

6- Copy the template name and apply the same on newly created site as follows
   $web.ApplyWebTemplate("{2752F8B3-5B44-4358-95A9-3D1DBFE55DE4}#AltoSite")

And Done!!!!!

2 comments :

  1. Have you run into situations where you lookup columns don't resolve properly? For a complex site, with lots of data and lookup columns to other lists, this approach has some side effects.

    ReplyDelete
    Replies
    1. Thanks for your feedback.
      I have run this script with moderate amount of data and having some lookups also but yes I haven't tested it with complex site.
      I will try to test it with complex site and put my result over here.

      Delete