I was on a project in the past where I was upgrading SharePoint 2007 to 2010.  Of course, our 2007 environment was full of Fab 40 templates.  In my experience, the sites would upgrade to 2010 successfully, but could never be moved to a new location since Microsoft was so kind as to not not support those in 2010.  After our 2010 upgrade, we had plans to completely reorganize our site because our content databases were getting huge.  Not being able to move over 100 Fab 40 sites was obviously a problem.  Fortunately, most of our sites were built off of the IT Team site template, but were not actually using much of the custom template functionality.  This meant that if I could just copy all the lists and libraries that had good content to a new site and leave behind the lists that I knew had custom code in them, I would be set. 

I really didn’t want to move all the lists and libraries for 100+ sites manually, so I turned to PowerShell.  The following script takes the current site URL, and the new site URL where the lists will go.  It looks to see if there are actually items in the list, and only moves lists and libraries that actually have content.

It also needs a directory on the server you run the PowerShell from that has enough space to hold a backup of the largest library you are moving as it is using backup-restore commands to make the move. 

I’ll paste in the full script, then call out some of what it is doing below.

   1:   #Add SharePoint PowerShell SnapIn if not already added
   2:  if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
   3:      Add-PSSnapin "Microsoft.SharePoint.PowerShell"
   4:  }
   6:  #This is the source web that is hosting the lists to move
   7:  $webApp = http://webapp
   8:  $shortName = "/path/to/your/source/site/"
   9:  $sourceWebUrl = $webApp + $shortName
  11:  #This is the destination web, where the lists will be copied to
  12:  $destWebUrl = http://webapp/path/to/your/destination/site/
  14:  #Location to store the export file
  15:  $path = "c:\SiteMoves\"
  17:  $web = Get-SPWeb $sourceWebUrl
  18:  $lists = $web.Lists | ?{$_.RootFolder.Url -ne "_catalogs/masterpage"}   
  20:   #Loop through the lists, export the list from the source, and import the list into the destination
  21:   foreach($list in $lists)
  22:   {
  23:      if($list.ItemCount -ge 1)
  24:      {
  25:       "Exporting " + $sourceWebUrl + $list.RootFolder.Url
  26:        export-spweb $sourceWebUrl -ItemUrl ($shortName + $list.RootFolder.Url) -IncludeVersions All -IncludeUserSecurity -NoFileCompression -path ($path + $list)
  27:       "Exporting complete."
  29:       "Importing " + $destWebUrl + $list.RootFolder.Url
  30:        import-spweb $destWebUrl -NoFileCompression -IncludeUserSecurity -path ($path + $list) 
  31:       "Importing Complete"
  32:       "`r`n"
  33:      }
  34:  }

Up to line 15 is just setting the global variables that will be used to get to our content.

Line 18 ignores the masterpage gallery.

Line 21 starts the loop to process each list found in the site.

Line 26 is building the URL to the list or library and performing a standard export

Line 30 is then importing the list or library back to the new site


 Let me know in the comments if you have any questions.


For more information on C5 Insight or this blog entry, please Contact Us.