For some laziness reasons, and mainly because I had so many issues (or fear of issues, you know how that is) with the PowerShell commands to manage site collections, I got used to stsadm command line, which is something from SharePoint 2007 a.k.a. MOSS.
It still works with SharePoint 2010, and also in SharePoint 2013 with some workaround. But clearly, the SharePoint Management Shell is the way to go now, and I have been advocating Powershell for long enough, to go and try to make the effort to use it here.
I got a request from a user, to move a document library with versioning enabled, from a site collection root to a subsite, while keeping the whole version history of all documents within.
Opening the document libraries (source & target) with Explorer and copy-pasting works and even keeps version history, but only within the same site collection. Moving documents this way to a subsite, removes the whole shebang. So yeah, it seems like we’ll have to use the dreaded Export-SPWeb command.
“Dreaded”, because the official documentation about it is poor, lowly documented and examples are just not complete, and you WILL get errors hardly understandable.
Blogger Blksthl provides a more documented version, but it still lacks to explain about this thing we use at work: managed paths. Even if in the URL nothing makes the difference, Managed Paths are not the same as site collections; they are a “sub-division of a web application”, and clearly they behave differently.
Logging on the App server with Farm Account (not sure such an elevation is needed, but I want to avoid permissions issues). The aim is to migrate the document library MyDocLib from the site collection http://webapp.mysite.be/managedpath1/MySiteColl, to the sub-site http://webapp.mysite.be/managedpath1/MySiteColl/MySubSite/, keeping all versions of all documents – and user security, of course.
Now let’s try various versions of the syntax, since it’s never indicated where to put the managed path:
|
PS D:\Backups> export-spweb -identity http://webapp.mysite.be/managedpath1/ -itemurl /MySiteColl/MyDocLib -path MyDocLib.cmp -IncludeVersions all -IncludeUserSecurity Export-SPWeb : Cannot find an SPWeb object with Id or Url : http://webapp.mysite.be/managedpath1/. At line:1 char:13 + export-spweb <<<< identity http://webapp.mysite.be/managedpath1/ -itemurl /MySiteColl/MyDocLib -path MyDocLib.cmp -IncludeVersions all -IncludeUserSecurity + CategoryInfo : InvalidData: (Microsoft.Share...CmdletExportWeb:SPCmdletExportWeb) [Export-SPWeb], SPCmdletPipeBindException + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletExportWeb |
Oh yeah, even the value for parameter -Includeversions is not even well documented in the official doc. They mention 4 possible values but not exactly what to enter for each of them. I had to get more info on this on another TechNet article…
|
PS D:\Backups> export-spweb -identity http://webapp.mysite.be -itemurl /managedpath1/MySiteColl/MyDocLib -path MyDocLib.cmp -IncludeVersions all -IncludeUserSecurity Export-SPWeb : The URL provided is invalid. Only valid URLs that are site collections or sites are allowed to be exported using stsadm.exe. At line:1 char:13 + export-spweb <<<< -identity http://webapp.mysite.be -itemurl /managedpath1/MySiteColl/MyDocLib -path MyDocLib.cmp -IncludeVersions all -IncludeUserSecurity + CategoryInfo : InvalidData: (Microsoft.Share...CmdletExportWeb:SPCmdletExportWeb) [Export-SPWeb], SPException + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletExportWeb |
The error message changes a bit, and it’s funny that it mentions the usage of stsadm.exe in the PowerShell command itself…
After some research, it might appear that the -itemurl parameter might require a full path, let’s try:
|
PS D:\Backups> export-spweb -identity http://webapp.mysite.be -itemurl http://webapp.mysite.be/managedpath1/MySiteColl/ -path MyDocLib.cmp -IncludeVersions all -IncludeUserSecurity Export-SPWeb : <nativehr>0x80070057</nativehr><nativestack></nativestack> At line:1 char:13 + export-spweb <<<< -identity http://webapp.mysite.be -itemurl http://webapp.mysite.be/managedpath1/MySiteColl/ -path MyDocLib.cmp -IncludeVersions all -IncludeUserSecurity + CategoryInfo : InvalidData: (Microsoft.Share...CmdletExportWeb:SPCmdletExportWeb) [Export-SPWeb], ArgumentException + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletExportWeb |
For some reason I didn’t include MydocLib in that command, and I don’t know if the result would be the same if I had. Anyway, you’ll find a lot of literature about this <nativehr>0x80070057</nativehr> error, but nothing I could get much from.
Ok enough playing, here is the way to success.
The problem is to know exactly what to put in –identity and -itemurl. For the first one, it looks like the full site collection path where the doc lib is located, is correct. For the second one, it’s another story, and thanks to TechNet user HAZET in yet another post on the problem, here is the SharePoint shell command to know about your site collection elements’ itemURLs:
|
$(Get-SPWeb -identity http://webapp.mysite.be/managedpath1/MySiteColl/).lists | ft title, @{Name="itemURL"; Expression = { $_.parentWebURL + "/" + $_.RootFolder}} |
The result will be something like:
|
Title itemURL ----- ------- Announcements /managedpath1/MySiteColl/Lists/Announcements Links /managedpath1/MySiteColl/Lists/Links Site Pages /managedpath1/MySiteColl/SitePages MyDocLib /managedpath1/MySiteColl/MyDocLib |
Okayyyyy so the Managed Path has to be indicated in the -itemURL. So here’s the final -and working- command:
|
PS D:\Backups> export-spweb -identity http://webapp.mysite.be/managedpath1/MySiteColl/ -itemurl /managedpath1/MySiteColl/MyDocLib -path MyDocLib.cmp -IncludeVersions all -IncludeUserSecurity |
-identity: full site collection path
-itemurl: document library path with managed path and a starting slash.
Now you have your .cmp file ready to be imported in the target sub-site. The Import-SpWeb command doesn’t need an -itemurl parameter so you might think your problems are over .. but not quite yet:
|
PS D:\Backups> import-spweb http://webapp.mysite.be/managedpath1/MySiteColl/NewSubSite -itemurl MyDocLib.cmp -IncludeUserSecurity Import-SPWeb : The file MyDocLib.cmp does not exist. At line:1 char:13 + import-spweb <<<< http://webapp.mysite.be/managedpath1/MySiteColl/NewSubSite -itemurl MyDocLib.cmp -IncludeUserSecurity + CategoryInfo : InvalidData: (Microsoft.Share...CmdletImportWeb:SPCmdletImportWeb) [Import-SPWeb], SPException + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletImportWeb |
The prompt is the correct directory, the file with this precise name exists. OK this is PowerShell so maybe .\MyDocLib.cmp ? Nah, same error message.
In fact… Export-SPWeb can work in the current directoty, but Import-SPWeb cannot (if anyone finds any logic in this, let me know). You need to specify the full path:
|
PS D:\Backups> import-spweb http://webapp.mysite.be/managedpath1/MySiteColl/NewSubSite -itemurl D:\Backup\MyDocLib.cmp -IncludeUserSecurity PS D:\Backups> |
It works!