Sunday, April 10, 2011

How to export/import website with .net

There are three options to export/import website in SharePoint. The most common used to be by Stsadm utility tool, now with SharePoint 2010 we have the option of doing it with Powershell. The third option is by c# and the SharePoint object model.

Here is an example for that :

 
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Deployment;


SPExportSettings exportSettings = new SPExportSettings();
exportSettings.SiteUrl = "http://oldwebsite";

exportSettings.ExportMethod = SPExportMethodType.ExportAll;
exportSettings.BaseFileName = "exportfile";
exportSettings.FileLocation = "c:\\";

SPExport export = new SPExport(exportSettings);
Export.Run();

SPImportSettings importSettings = new SPImportSettings;
importSettings.BaseFileName = "exportfile";
importSettings.FileLocation = "c:\\";
importSettings.SiteUrl = "http://newwebsite";
SPImport import = new SPImport(importSettings);
Import.Run();