我遇到了sharepoint.deployment.spimport的压缩问题。
我想在同一网站集中复制一个网址:
MYSERVER / mysitecoll / WEBSITE1
MYSERVER / mysitecoll / WEBSITE2
当我使用PowerShell命令执行此操作时,它完美地完成,网站2与网站1相同
Export-SPWeb -Identity http://myserver/mysitecoll/website1 -Path D:\mybackups\mytestsave\mybackup.bak
和
Import-SPWeb -Identity http://myserver/mysitecoll/website2 -Path D:\mybackups\mytestsave\mybackup.bak
但我需要用c#我用
做同样的事情 private void ExportSpWeb()
{
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = SPContext.Current.Web;
SPExportObject exportObject = new SPExportObject();
exportObject.Id = myWeb.ID;
exportObject.ParentId = mySite.ID;
exportObject.Type = SPDeploymentObjectType.Web;
SPExportSettings settings = new SPExportSettings();
settings.SiteUrl = mySite.Url;
settings.ExportMethod = SPExportMethodType.ExportAll;
settings.FileLocation = "D:\\mybackups\\mytestsave";
settings.BaseFileName = "test.cab";
settings.FileCompression = true;
settings.ExcludeDependencies = true;
settings.CommandLineVerbose = true;
settings.ExportObjects.Add(exportObject);
SPExport export = new SPExport(settings);
export.Run();
}
private void importSpWeb()
{
SPSite mySite = SPContext.Current.Site;
SPWeb myDestWeb = mySite.AllWebs["website2"];
SPImportSettings impsettings = new SPImportSettings();
impsettings.SiteUrl = mySite.Url;
impsettings.LogFilePath = "D:\\mybackups\\mytestsave\\test.log";
impsettings.WebUrl = myDestWeb.ServerRelativeUrl;
impsettings.FileLocation = "D:\\mybackups\\mytestsave";
impsettings.FileCompression = true;
impsettings.BaseFileName = "test.cab";
impsettings.RetainObjectIdentity = false;
SPImport import = new SPImport(impsettings);
import.Run();
}
但是comportment与PowerShell不同:不是使用指定的WebUrl设置(http://myserver/mysitecoll/website2)创建的, 导入的网站将创建为具有路径的新子网站 http://myserver/mysitecoll/website2/website1
我应该如何编辑代码以获得与PowerShell相同的结果?
答案 0 :(得分:0)
这个问题让我深思熟虑,因为我像你一样碰到了墙,但这让我想出了一个问题:Where is definition of sharepoint cmdlets and How to get their implementation?
因此,知道Import-SPWeb
中SPCmdletImportWeb
又名Microsoft.SharePoint.PowerShell.dll
的知识,我检查了它是如何完成的。
棘手的部分是,由于某种原因SPImportWeb
有一些奇怪的逻辑来修改WebUrl属性并始终将/
添加到最后。因此,在SPCmdletImportWeb
中,他们使用SPImport
类Created
事件来重置某些属性。
对于您的情况,当您导出和导入一个SPWeb对象时,您需要在导入对象中添加以下代码:
string webUrl = "website2";
// your stuff
SPImport import = new SPImport(impsettings);
import.Started += delegate(object sender, SPDeploymentEventArgs args)
{
SPImportObjectCollection rootObjects = args.RootObjects;
if (rootObjects[0].Type == SPDeploymentObjectType.Web)
{
rootObjects[0].TargetParentUrl = site.Url;
rootObjects[0].TargetName = webUrl;
return;
}
};
要查找SPCmdletImportWeb
获取ILSpy的完整代码,请在第一个网址中关注我的迷你教程。
完整的测试代码:
[TestMethod]
public void Test_ExportSpWeb()
{
ExportSpWeb("http://lab/sites/custom-dev", "website1", @"C:\temp\bak\bak2.bak");
}
[TestMethod]
public void Test_ImportSpWeb()
{
ImportSpWeb("http://lab/sites/custom-dev", "website2", @"C:\temp\bak\bak2.bak");
}
private void ImportSpWeb(string siteUrl, string webUrl, string path)
{
using (SPSite site = new SPSite(siteUrl))
using (SPWeb web = site.OpenWeb(webUrl))
{
SPImportSettings impsettings = new SPImportSettings();
impsettings.SiteUrl = site.Url;
impsettings.LogFilePath = path + ".log";
impsettings.WebUrl = web.ServerRelativeUrl + "/" + webUrl;
impsettings.FileLocation = Path.GetDirectoryName(path);
impsettings.FileCompression = true;
impsettings.CommandLineVerbose = true;
impsettings.BaseFileName = Path.GetFileName(path);
impsettings.RetainObjectIdentity = false;
SPImport import = new SPImport(impsettings);
import.Started += delegate(object sender, SPDeploymentEventArgs args)
{
SPImportObjectCollection rootObjects = args.RootObjects;
if (rootObjects[0].Type == SPDeploymentObjectType.Web)
{
rootObjects[0].TargetParentUrl = site.Url;
rootObjects[0].TargetName = webUrl;
return;
}
};
import.Run();
}
}
private void ExportSpWeb(string siteUrl, string webUrl, string path)
{
using (SPSite site = new SPSite(siteUrl))
using (SPWeb web = site.OpenWeb(webUrl))
{
SPExportObject exportObject = new SPExportObject();
exportObject.Id = web.ID;
exportObject.ParentId = site.ID;
exportObject.Type = SPDeploymentObjectType.Web;
SPExportSettings settings = new SPExportSettings();
settings.SiteUrl = site.Url;
settings.ExportMethod = SPExportMethodType.ExportAll;
settings.FileLocation = Path.GetDirectoryName(path);
settings.BaseFileName = Path.GetFileName(path);
settings.LogFilePath = path + ".log";
settings.FileCompression = true;
settings.ExcludeDependencies = true;
settings.CommandLineVerbose = true;
settings.ExportObjects.Add(exportObject);
SPExport export = new SPExport(settings);
export.Run();
}
}