SharePoint 2007: How to Export/Import a Site Programmatically
Introduction:
We received a request from site admins that they do not want to use STSADM commands to export/import a site since they do it frequently for many sites. They also wanted to do it in a single shot without running the command again for import after export. Hence we created the following console application that solves this issue. Here is the code:
C# Code Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Deployment;
namespace SiteExportImport
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please input the source site URL ");
//Enter URL of your source site
String sourceSite = Console.ReadLine();
Console.WriteLine("Please input the destination site URL");
//Enter URL of your destination site
String destinationSite = Console.ReadLine();
Console.WriteLine("Please input the destination file location");
//Enter the location where you want your exported files to be generated
String fileLocation = Console.ReadLine();
Console.WriteLine("Please input the Base file name");
//Enter the name of your exported file
String baseFileName = Console.ReadLine();
Console.WriteLine("Please input the export log file location");
//Enter full path including the name of your export log file
String exportlogLocation = Console.ReadLine();
Console.WriteLine("Please input the import log file location");
//Enter full path including the name of your import log file
String importlogLocation = Console.ReadLine();
SPImport import = new SPImport();
SPExport export = new SPExport();
SPExportSettings exportSettings = export.Settings;
exportSettings.SiteUrl = sourceSite;
exportSettings.CommandLineVerbose = true;
exportSettings.ExportMethod = SPExportMethodType.ExportAll;
exportSettings.FileLocation = fileLocation;
exportSettings.BaseFileName = baseFileName;
exportSettings.LogFilePath = exportlogLocation;
exportSettings.OverwriteExistingDataFile = true;
export.Run();
Console.WriteLine("Export Completed");
SPImportSettings importSettings = import.Settings;
importSettings.BaseFileName = baseFileName;
importSettings.CommandLineVerbose = true;
importSettings.FileLocation = fileLocation;
importSettings.LogFilePath = importlogLocation;
importSettings.SiteUrl = destinationSite;
import.Run();
Console.WriteLine("Import Completed");
Console.ReadLine();
}
}
}
The first requirement for this is to import the dll Microsoft. Sharepoint it is available in the folder ISAPI in 12 hive folder. The input values along with their significance are given as comments.
This can be modified further as per the needs. This can be executed from any WFE on a farm.
I hope this will help you out.
Thanks,
Rahul Rashu