Pages

Tuesday, February 7, 2012

Export and Import Site in Sharepoint

 Many have faced a situation of exporting a site and importing in another site collection. There are times where you need to move a subsite in a site collection to another site collection or into a subsite of same site collection. Recently I was involved in the project where the client moves the site into a subsite or reversal more often. They in fact  wanted this as a part of the application itself. Further, they don't want to run STSADM or power shell command and depend on IT for doing this. To add complexity to the situation the client had 2 different environment

1. Hosted externally
2. Hosted Internally

The reason for having this set up is the client do not have a big IT team to manage the infrastructure as external environment hosts some of there client information and they wanted to have backup and disaster recovery capabilities, which they don't want to own.

Having given the background of situation now let us see how we can achieve this operation. The easiest solution I think and which I worked on to implement was using Sharepoint webservices. This I think is good solution because there is less code to manage and there is a huge support from Microsoft as it is there code and this approach can also be used from Client object model.

Code :

                   SharepointSiteServices.Sites siteWs = new SharepointSiteServices.Sites();
                   siteWs.Url = SPContext.Current.Web.Url + "/_vti_bin/sites.asmx";

                   siteWs.Credentials = System.Net.CredentialCache.DefaultCredentials;
                  string[] datafiles = new string [] { site.Url + "/TemplateLibrary/"+ SPContext.Current.Web.Name +".cmp"};
                  int i = siteWs.ExportWeb(SPContext.Current.Web.Name, SPContext.Current.Web.Url, site.Url + "/TemplateLibrary", true, true, true, Int32.MaxValue);
                  siteWs.Url = site.Url + "/_vti_bin/sites.asmx";
                  int j = siteWs.ImportWeb(SPContext.Current.Web.Name, site.Url + "/" + SPContext.Current.Web.Name, datafiles, site.Url + "/TemplateLibrary", true, true);

The code above shows how you can export and import the site or subsite. But the things that needs to considered are

There is minimum documentation for the web service calls export and import opeartion. I managed to get a PDF document that contains the error codes and implementation details from technet after tearing google away for any sort of information. Finally i got hold of a former microsoft guy and he shared this PDF with me. I am uploading it any body else who need it may use it.
  • Web services needs to pointed to the site that needs to exported
  • set the web service credentials. May be you can run with elevated privileges if you know this operation is going to be performed by some one else in the system other than administrator.
  • datafiles which is string array represents the file name that you want to use when exporting the site. I am using the default site name as file name here. You can change this to any thing you want. Important thing that needs to conscious of is the path of the file name should be a SharePoint library. It does not let you pick a directory path.
  • Change the Site URL for the import site and now you can import the sites.
 Reference

Sites Web Service

No comments:

Post a Comment