Export Import SharePoint Site using Web Service

nProblem:
n
n

nWhile working with SharePoint Online – sometimes it isnrequired to take the backup of the site or to export the site to some othernenvironment, but due to no access to SharePoint server, we end up with writingnhuge code to migrate the site contents to another site using any client objectnmodel.

n

nIf server side scenario is considered, we have lots ofnoptions to perform a simple site export and import operation , like say – usenstsadm commands , use Central Administration , use powershell scripts but fornSharePoint Online environment what to do? Well here comes web services innpicture , how? We will see in details.

n

n

n

nSolution:

n

nSharePoint Web services are already there since MOSS 2007,nbut mostly not used. Here we are going to use the sites.asmx web service tonexport and import the sites.

n

nNote that before performing these operations using webnservices – make sure that you have site collection administrator rights onnsource as well as target site.

n

nI created a Team site as source site and added somensample data to it , say about 4000 documents + 3000 list items with attachmentsn+ some content types + some site columns + some lookup columns + some list andncontent type workflows.

n

n

n

nWe can use the ExportWeb function given by thensites.asmx web service which takes some parameters as input – let’s see what thosenare.

n

    n

  • 1.       jobName:nthis is the string parameter and can be set to any string , using this name thencmp files will be created. Please make a note that no special characters arenallowed here.
  • n

  • 2.       webUrl:nurl of the site which needs to be exported as cmp files.
  • n

  • 3.       Datapath:npath where all cmp files will be stored (typically a path of the documentnlibrary on source site)
  • n

  • 4.       Includesecurity:nas name suggests – Boolean parameter to include security or not
  • n

  • 5.       Overwrite:nBoolean parameter to overwrite cmp files on save location
  • n

  • 6.       cabSize:nsize of each cmp file , can be set to 2GB max.
  • n

  • Export web function returns an integer afterncompletion , but mostly the description is not available for the return types. 
  • n

n

nHere it is :

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

nValue

n

nDescription

n

n1

n

nPending:n The operation is in progress.

n

n4

n

nInvalidExportUrl:n The site specified in the webUrl is not accessible.

n

n5

n

nExportFileNoAccess:n The location specified in the dataPath is not accessible.

n

n6

n

nExportWebNoAccess:n The user does not have the required permissions to execute this operationn successfully. The user MUST have the ManageLists and AddListItems permissionsn in the solution gallery.

n

n7

n

nExportError:n An error other than the errors listed in this table occurred during exportingn the site. An example of a condition that would result in this error is ifn there was no content on the site to export.

n

n8

n

nUploadDataError:n The content migration package file is not uploaded to the servern successfully. This can occur if the specified path was invalid, read-only,n the caller is not authorized to access it, or the file is already open.

n

n

n

n

n

n

nImportWeb:

n

nWhile importing the web from cmp files using webnservices – we can use ImportWeb function of sites.asmx web service.

n

n

n

nImportantnpart here is – the target site on which import is to be done – should bencreated using OOB blank site definition. There can be an error while performingnthe import operation like – the source site is created using template STS#0 andntarget site is created using STS#1 , – solution to this is – create target sitencollection without any template – select the select template later option whilencreating target the site collection.

n

nImportWeb function take some parameters let’s see whatnthose are

n

    n

  • 1.       jobName:nstring parameter , can be set to anything – but no special characters should benincluded.
  • n

  • 2.       webUrl:nurl of target web.
  • n

  • 3.       dataFiles:nthis is array of string containing url of all cmp files. Example : If there aren10 cmp files in the source location – then dataFiles parameter will be anynarray having urls of 10 files in ascending order.
  • n

  • 4.       logPath:npath where log file can be created. This can be passed as null. (typically anurl of any document library)
  • n

  • 5.       includesecurity:nto include security or not
  • n

  • 6.       overwrite:noverwrite log files.
  • n

n

nSimilar to ExportWeb function – ImportWeb functionnalso return an interger on completion.

n

nHere are the details

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

nValue

n

nDescription

n

n1

n

nPending:n The operation is in progress.

n

n2

n

nGenericError:n An error other than the errors listed in this table occurred importing then site.

n

n4

n

nInvalidImportUrl:n The site specified in the webUrl is not accessible.

n

n5

n

nImportFileNoAccess:n At least one location specified in dataFiles is not accessible.

n

n6

n

nImportWebNoAccess:n The user has insufficient permission to import to the location specified inn webUrl.

n

n8

n

nmportWebNotEmpty:n The location specified by webUrl corresponds to an existing site that is notn a blank site.

n

n11

n

nLogFileNoAccess:n The location specified by logPath is not accessible.

n

n

n

n

nOk , so after lots of theory – here is the simple consolenapplication example which I tried on my environment successfully.

n

n

n

nstatic void Main(string[] args)

n

n{

n

n   nstringnsourcesiteurl = “http://sourcesitecollection/”;

n

n   nstringntargetsiteurl = “http://targetblanksitecollection/”;

n

n    string svcUrl = “_vti_bin/sites.asmx”;

n

n

n

n   nSites sites = new Sites();

n

n   nNetworkCredentialncredentials = new NetworkCredential(“username”, “password”,n“domain”);

n

n    sites.Credentialsn= credentials;

n

n    sites.Timeoutn= 600000;

n

n

n

n   nConsole.WriteLine(“Choose..1. Export , 2. Import”);

n

n    string responce = Console.ReadLine();

n

n

n

n    if (responce.Equals(“1”))

n

n    {

n

n       sites.Urln= string.Format(“{0}{1}”,nsourcesiteurl, svcUrl);

n

n       try

n

n       {

n

n         int result = sites.ExportWeb(“MyExportTest”,nsourcesiteurl, “http://sourcesite/Exports”,ntrue, true, true, 1024);

n

n         Console.WriteLine(“donenwith result – “ + result.ToString());

n

n       }

n

n       catch (Exceptionnex)

n

n       {

n

n          Console.WriteLine(ex.Message);

n

n          Console.WriteLine();

n

n          Console.WriteLine(ex.StackTrace);

n

n       }

n

n     }

n

n

n

n     else ifn(responce.Equals(“2”))

n

n     {

n

n         sites.Url = string.Format(“{0}{1}”, targetsiteurl, svcUrl);

n

n         string[] dataFiles = newnstring[] { “http://sourcesite/Exports/MyExportTest.cmp”n};

n

n         

n

n          try

n

n          {

n

n            int result = sites.ImportWeb(“MyExportTest”,ntargetsiteurl, dataFiles, null, true, true);

n

n            Console.WriteLine(“donenwith result – “ + result.ToString());

n

n          }

n

n          catch (Exceptionnex)

n

n          {

n

n            Console.WriteLine(ex.Message);

n

n            Console.WriteLine();

n

n            Console.WriteLine(ex.StackTrace);

n

n          }

n

n       }

n

n     Console.ReadLine();

n

n   }

n

n

n

n

n

n

n

n

Leave a Comment