2017-05-26 46 views
1

我有一個sharepoint.deployment.spimport comportment的問題。如何使用c#導入SPWeb以具有與PowerShell相同的行爲?

我想在同一網站集中像中複製網頁:

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(); 
    } 

但行爲舉止是不一樣的PowerShell:而不是使用指定的WEBURL設置創建(http://myserver/mysitecoll/website2) , 導入的網站創建爲一個新的子網站與路徑 http://myserver/mysitecoll/website2/website1

如何編輯我的代碼以獲取與PowerShell相同的結果?

+0

做我的回答解決您的問題? – tinamou

+0

您好,首先感謝您的回答,目前,我們已經決定使用「網站定義」,但是,它可能會讓我的企業中的SharePoint專家開發人員感興趣,以獲得答案。 –

回答

0

這個問題讓我思考很深刻,因爲我打的牆,你做了,但是這讓我想出一個quesion:Where is definition of sharepoint cmdlets and How to get their implementation?

因此,有知識,其中在Microsoft.SharePoint.PowerShell.dllImport-SPWeb又名SPCmdletImportWeb是我查它是如何完成的。

棘手的部分是,由於某種原因SPImportWeb有一些奇怪的邏輯來修改WebUrl屬性,總是添加/到最後。因此,在SPCmdletImportWeb中,他們使用SPImportCreated事件來重置某些屬性。

對於你的情況,當你導出和導入一個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(); 
    } 
} 
+0

我嘗試你的答案,完美的工作,非常感謝你:) –

+0

很高興爲你工作。請將此標記爲已回答。 – tinamou

相關問題