Art Vandelay – Import/Export SharePoint Content in C#

Here is some handy dandy code for Exporting content from a SharePoint site and importing into another.

 
 

 
 

using System;

using System.Collections.Generic;

using System.Text;

 
 

namespace ContentMoveTest

{

class
Program

{

static
void Main(string[] args)

{

ExportImport(http://www.source.com, http://www.target.com);

}

 
 

 
 

static
void ExportImport(string sourceUrl, string destinationUrl)

{

string fileName = Export(sourceUrl);

Console.WriteLine(“Local filename for this export/import is {0}”, fileName);

Import(destinationUrl, fileName);

}

 
 

static
string Export(string siteURL)

{

Microsoft.SharePoint.Deployment.SPExportSettings exportSettings = new Microsoft.SharePoint.Deployment.SPExportSettings();

 
 

exportSettings.AutoGenerateDataFileName = true;

exportSettings.ExportMethod = Microsoft.SharePoint.Deployment.SPExportMethodType.ExportAll;

//exportSettings.ExportMethod = Microsoft.SharePoint.Deployment.SPExportMethodType.ExportChanges;

exportSettings.CommandLineVerbose = true;

exportSettings.SiteUrl = siteURL;

exportSettings.IncludeSecurity = Microsoft.SharePoint.Deployment.SPIncludeSecurity.All;

exportSettings.IncludeVersions = Microsoft.SharePoint.Deployment.SPIncludeVersions.All;

Microsoft.SharePoint.Deployment.SPExport export = new Microsoft.SharePoint.Deployment.SPExport(exportSettings);

Console.WriteLine(“Starting export of URL {0}”, siteURL);

export.Run();

return exportSettings.FileLocation + “\\” + exportSettings.BaseFileName;

}

 
 

static
void Import(string siteURL, string fileToImport)

{

Microsoft.SharePoint.Deployment.SPImportSettings importSettings = new Microsoft.SharePoint.Deployment.SPImportSettings();

 
 

importSettings.BaseFileName = System.IO.Path.GetFileName(fileToImport);

importSettings.FileLocation = System.IO.Path.GetDirectoryName(fileToImport);

importSettings.CommandLineVerbose = true;

importSettings.SiteUrl = siteURL;

importSettings.RetainObjectIdentity = true;

importSettings.IncludeSecurity = Microsoft.SharePoint.Deployment.SPIncludeSecurity.All;

importSettings.UpdateVersions = Microsoft.SharePoint.Deployment.SPUpdateVersions.Append;

importSettings.UserInfoDateTime = Microsoft.SharePoint.Deployment.SPImportUserInfoDateTimeOption.ImportAll;

Microsoft.SharePoint.Deployment.SPImport import = new Microsoft.SharePoint.Deployment.SPImport(importSettings);

Console.WriteLine(“Starting import to URL {0}”, siteURL);

import.Run();

}

 
 

}

}

Advertisements

2 Responses to “Art Vandelay – Import/Export SharePoint Content in C#”

  1. Frank Cardillo Says:

    You you the term content when describing what it moves. are you saying that you could use this to move a specific list or library rather than an entire site?

    Thanks

    Frank


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: