How to Export/Import a Site Programmatically in SharePoint 2007

How to Export/Import a Site Programmatically in SharePoint 2007



Introduction:


I 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 I 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 in a farm.


I hope this will help you out.


Thanks,
Rahul Rashu
Leave a Comment
  • Please add 1 and 8 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Fernando Lugão Veltem edited Revision 3. Comment: remove en-us from title

  • Craig Lussier edited Revision 2. Comment: added en-US to tags and title

  • Craig Lussier edited Revision 1. Comment: added headings and toc

Page 1 of 1 (3 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • It would be nice to see a PowerShell example. Even though the target is SharePoint 2007, this is certainly possible...

  • Hi Craig,

    Thanks for your valuable comment. I have created a powershell script that works in the same way. Have a look at this social.technet.microsoft.com/.../how-to-export-and-import-a-site-using-powershell-script-in-sharepoint-2007-and-sharepoint-2010.aspx

    I hope this will help you out.

    Thanks,

    Rahul Rashu

  • Your contribution to the community is appreciated. Cheers. C.

  • Craig Lussier edited Revision 1. Comment: added headings and toc

  • Craig Lussier edited Revision 2. Comment: added en-US to tags and title

  • Nice, thank You!

  • Can You please give examples of input values? I see log and suppose that it works wrong for me(exports all sites together and I don't understand where)

  • Hi Rockie,

    The first input is the URL of source site.

    The second one is the URL of destination site.

    The third one is the location where you want to save the exported files. For example:-F:\backup

    The fourth one is the name of the base backup file you want for example export.bak.

    The fifth and sixth ones are the locations of your export and import log file location along with the file names.

    Do let me know if it helps you out.

    Thanks,

    Rahul Rashu

  • Fernando Lugão Veltem edited Revision 3. Comment: remove en-us from title

Page 1 of 1 (9 items)