Sharepoint 复制备份系列--使用编程方式复制列表2(Copy a SharePoint List or site ,web Programmatically)
微软sharpoint server 2007和windows sharepoint services 3.0是一个相当优秀的工具,帮助企业组织公司的日常内容并且可以有效的应用于业务之中。但是所有的存在的信息怎么样保存和移动呢?虽然说2007和wss是一个很好的平台,但是处理这些问题,可以操作的工具还是比较不足的。这篇文章将会介绍解决这些问题的一些方法,从一个位置复制或者移动到另一个位置。
我们知道Windows SharePoint Services 3。0里面有这样的一个类,它支持从windows sharepoint services 网站集导出特定的内容为一个CAB文件形式(扩展文件名),它是以xml形式的存在。
SPExport 类 (Microsoft.SharePoint.Deployment)
命名空间: Microsoft.SharePoint.Deployment
程序集: Microsoft.SharePoint (在 microsoft.sharepoint.dll 中)
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)]
public sealed class SPExport : SPDeployment
备注
这个类可以用来导出,导入,发布,迁移sharepoint 的内容,支持备份和恢复。
但需要设置Spexportsettings。
You can initiate an export operation by first initializing an instance of the Microsoft.SharePoint.Deployment.SPExportSettings class with the required export settings and then passing the SPExportSettings object to the constructor of SPExport class; you then call the SPExport.Run method.
示例
The following code example demonstrates how to perform a full export of an entire site collection.
SPExportMethodType.ExportAll; 可选。枚举,指定导出操作是完全导出还是增量导出(根据自上一次导出以来所发生的变化)。
ExportAll |
指定应将所有导出对象导出到内容迁移数据包。 |
ExportChanges |
指定只应导出自内容迁移数据包的上一版本后发生了更改的导出对象。 |
settings.IncludeSecurity = SPIncludeSecurity.All;
None |
指定未迁移任何用户或组信息。(默认) |
WssOnly |
仅应用 Windows SharePoint Services 3.0 安全设置。包括用户成员资格和角色分配(如默认角色,例如,网站设计者或从默认角色扩展的任何自定义角色)。迁移每个对象的访问控制列表 (ACL)。不包括 DAP 或 LDAP 服务器中定义的任何用户信息。 |
All |
应用所有可用的 Windows SharePoint Services 3.0 安全设置。包括用户成员资格和角色分配(如默认角色,例如,网站设计者或从默认角色扩展的任何自定义角色)。迁移每个对象的 ACL。此外,还包括在 DAP 或 LDAP 服务器中定义的用户信息。 |
[C#]
SPExportSettings settings = new SPExportSettings();
settings.SiteUrl = "http://servername:80";
settings.FileLocation = @"c:\exportdata";
settings.BaseFileName = "exportfile.cmp";
settings.FileCompression = true;
settings.ExportMethod = SPExportMethodType.ExportAll;
SPExport export = new SPExport(settings);
export.Run();
The following code example demonstrates how to perform an incremental export. Notice that the code sets the ExportMethod property to ExportChanges and then provides a change token.
[C#]
settings.ExportMethod = SPExportMethodType.ExportChanges;
settings.ExportChangeToken = "1;1;87a71761-2987-48eb-9d29-48428270e01;632937036861200000;5512";
SPExport/SPImport
SPExport/SPImport在Microsoft.SharePoint.Deployment命名空间里,这些类提供了一些方法来做一些基本的备份,恢复网站,列表和其他对象,还有一些在sharepoint designer 使用的也可以用这个方法。要使用这两个类,你必须要配置settings这个类。
以下是一个main方法:
static void Main(string[] args)
{
int readValues;
char charValues;
Console.WriteLine("Please enter the source web url:");
string sourceUrl = Console.ReadLine();
Console.WriteLine("Please Enter the destination web url:");
string destUrl = Console.ReadLine();
Console.WriteLine("Please Enter the list your want to copy:");
string copyList = Console.ReadLine();
string retval = "Hello,the source web url:" + sourceUrl + ", the destination web url:" + destUrl + ",the list:"+copyList;
Console.WriteLine(retval);
Console.WriteLine("Let us begin to copy: y(yes) or n(No)");
readValues = Console.Read();
charValues = (char)readValues;
char yesTag = 'y';
if (charValues != yesTag)
{
return;
}
string filePath = @"C:\Export Files\" + copyList + ".cmp";
if (File.Exists(filePath))
{
Console.WriteLine( copyList + ".cmp"+ "is existd,please check and do again!");
Thread.Sleep(3000);
return;
}
ExportMethod(sourceUrl,copyList);
ImportMethod(destUrl, copyList);
Console.WriteLine("Copy:"+copyList+" task is done.");
Console.WriteLine(retval);
Thread.Sleep(10000);
}
T As stated above, the export process is configured using the settings class, SPExportSettings in this case.
public static void ExportMethod(string sourceUrl,string copyList)
{
// Get handle to web and doclib to export
SPSite site = new SPSite(@sourceUrl);
SPWeb web = site.OpenWeb();
SPList list = web.GetList("/itws/Lists/" + copyList);
// Define export settings
SPExportSettings settings = new SPExportSettings();
settings.ExcludeDependencies = true;
settings.SiteUrl = sourceUrl;
settings.FileLocation = @"C:\Export Files\";
settings.BaseFileName = copyList+".cmp";
settings.ExportMethod = SPExportMethodType.ExportAll;
settings.LogFilePath = settings.FileLocation + copyList+"_export_log.txt";
settings.CommandLineVerbose = true;
settings.IncludeSecurity = SPIncludeSecurity.All;
// Add reference to document library to export
SPExportObject exportObject = new SPExportObject(list.ID, SPDeploymentObjectType.List, web.ID, false);
settings.ExportObjects.Add(exportObject);
// Export it
SPExport export = new SPExport(settings);
try
{
export.Run();
}
catch (Microsoft.SharePoint.SPException ex)
{
throw ex;
}
}
同样,导入的时候也要设置SPImportSettings
public static void ImportMethod(string destUrl,string copyList)
{
// Settings for import
SPImportSettings settings = new SPImportSettings();
// File & path
settings.FileLocation = @"C:\Export Files\";
settings.BaseFileName = copyList+".cmp";
// Site and web to import to
settings.SiteUrl = destUrl;
settings.WebUrl = destUrl;
// Set log file location
settings.LogFilePath = settings.FileLocation + copyList+"_import_log.txt";
// Display messages while running
settings.CommandLineVerbose = true;
settings.IncludeSecurity = SPIncludeSecurity.All;
// Don't retain object GUIDs, only necessary
// if want to do incremental imports to same list
// at a later time
settings.RetainObjectIdentity = false;
// Keep security, versions, and date/time stamps
settings.UpdateVersions = SPUpdateVersions.Append;
settings.UserInfoDateTime = SPImportUserInfoDateTimeOption.ImportAll;
// Import it
SPImport import = new SPImport(settings);
try
{
import.Run();
}
catch (SPException ex)
{
throw ex;
}
}