Export-SPWeb Syntax Changes Between Root Site and Sub Sites


Working on a somewhat related issue today and needed to test something with Export-SPWeb. We were trying to import a list from an STP (List template) and in order to get there I decided to export one of my own lists and play around a bit. Much to my surprise I was unable to export a custom list from my lab environment. I kept getting the following error:

 

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:1 
 + export-spweb $web -Path exported_list.cmp -ItemUrl "/lists/testlist" 
 -Force 
 + 
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
 + CategoryInfo : InvalidData: (Microsoft.Share...CmdletExportWeb: 
 SPCmdletExportWeb) [Export-SPWeb], SPException 
 + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletExportWeb

 

It was quite maddening as the parameters are not that difficult to figure out. (get-help export-spweb)

So I was trying many things and discovered that in a second lab environment I had that it worked flawlessly and I was determined to discover why. The first thing that a peer of mine found for me was that in the failing environment the web from which I was trying to export the custom list was on a site collection under the ‘sites’ managed path (in other words not the root site). The environment that was working was the root site collection.

To cut to the chase, you may not be able to use the same syntax depending on where the list resides. The parameter in question that kept failing for me was the ItemUrl parameter. It is to represent the url to the list that you would like to export. Basically if the site collection is the root site collection, then the ItemUrl should contain a leading “/” such as this:

Export-SPWeb http://intranet.contoso.lab –Path exported_list.cmp –ItemUrl “/Lists/TestList”

However if the site collection is not the root, then you should either include the full server relative url to the list or the web relative url to the list *without* the leading slash:

Export-SPWeb http://intranet.contoso.lab/sites/TestImport –Path exported_list.cmp –ItemUrl “/sites/TestImport/Lists/TestList”

or

Export-SPWeb http://intranet.contoso.lab/sites/TestImport –Pat exported_list.cmp –ItemUrl “Lists/TestList”

If you care to know, basically under the covers the Export-SPWeb cmdlet will call SPWeb.GetList(), but prior to calling that function it will prepend the SPWeb.ServerRelativeUrl and an extra “/” if the string in ItemUrl does not have a leading “/”. In short, to be safe if you always set the ItemUrl parameter to the server relative url and then path to the list, it should always work regardless of web location.


Comments (9)
  1. BlueSky2010 says:

    Thanks for blogging this Brian…now I know why I ran into this once before.

  2. BrianGre says:

    Glad it helped!

  3. Vinay says:

    It worked for me. Thanks Brain.

  4. vinay ayinapurapu says:

    it worked

  5. Muhammad shahid says:

    Export-SPWeb intranet.contoso.lab/…/TestImport –Path exported_list.cmp –ItemUrl “/sites/TestImport/Lists/TestList”

    worked for me

    thanksssss

  6. Manoj says:

    Thanks for sharing this Brian

  7. JP says:

    Thanks for posting this Brian.  I usually work with host-named site collections so always used the leading /.  Couldn't figure out why it wasn't working with a subsite.

  8. I posted this on StackOverflow and the MSDN SharePoint forum, so let he relay this here (its not directed at you Brian):

    I believe there are actually two separate errors which occur behind the scenes and IMHO that Microsoft should fix them:

    1. When dealing with the `Identity` and it is just the base URL `http://sharepoint.contoso.com` and a slash in `ItemUrl` is not added the `Export-SPWeb` has an internally (*read unhandled*) error of `Export-SPWeb : 0x80070057` which meands nothing to an end user and is clearly a bug.

    2. When the user adds a `/` to a relative pathed lcation `Export-SPWeb` can’t handle it and reports ` The URL provided is invalid. Only valid URLs that are site collections or sites are allowed to be exported using stsadm.exe.`

    The second error is not a full failure and is more useful. But it should not mention `stsadm.exe` for site administration at all and handle a ‘/’ in the pathing.

  9. Gurudatt says:

    Thanks for valuable info. This worked as charm 🙂

Comments are closed.

Skip to main content