2011-12-02 44 views
3

我正在嘗試使用sitecore API來序列化和還原sitecore項目。我創建了一個WCF應用程序來檢索給定ID或sitecore路徑(/ sitecore/content/home)的項目名稱,檢索子項給出id或路徑的項目名稱列表。我也可以序列化內容樹。如何使用Sitecore.Data.Serialization.Manager.LoadItem(path,LoadOptions)將項目恢復到Sitecore?

public void BackupItemTree(string id) 
    { 
     Database db = Sitecore.Configuration.Factory.GetDatabase("master"); 
     Item itm = db.GetItem(id); 

     Sitecore.Data.Serialization.Manager.DumpTree(itm); 
    } 

上面的代碼很好用。運行後可以看到內容樹已被序列化。

然而,當我試圖還原序列化項目期運用如下:

public void RestoreItemTree(string path) 
    { 
     try 
     { 
      using (new Sitecore.SecurityModel.SecurityDisabler()) 
      { 
       Database db = Sitecore.Configuration.Factory.GetDatabase("master"); 
       Data.Serialization.LoadOptions opt = new Data.Serialization.LoadOptions(db); 
       opt.ForceUpdate = true; 

       Sitecore.Data.Serialization.Manager.LoadItem(path, opt); 
       //Sitecore.Data.Serialization.Manager.LoadTree(path, opt); 
      } 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
    } 

有了這個代碼,我沒有得到任何錯誤。它運行,但如果我檢查SiteCore它沒有做任何事情。我已經使用Office Core示例進行了測試。我發送的路徑可能是這樣的:

C:\ inetpub \ wwwroot \ sitecoretest \ Data \ serialization \ master \ sitecore \ content \ Home \ Standard-Items \ Teasers \ Our-Clients.item

C:\的Inetpub \ wwwroot的\ sitecorebfahnestockinet \ DATA \序列\主\ Sitecore的\內容\首頁\標準項\ \玩笑我們的客戶端

似乎都不做任何事情。我改變了該項目的預告片標題,並試圖在每次改變仍然存在之前恢復。

任何幫助將不勝感激,因爲SiteCore文檔是非常有限的。

回答

1

您擁有正確的LoadOptions用於強制覆蓋(又名Revert)。

我懷疑你用於.item文件的路徑有誤。我會建議修改您的方法以獲取Sitecore項目的路徑。使用該路徑,您應該利用其他序列化API來確定文件的位置。

public void RestoreItemTree(string itemPath) 
{ 
    Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("master"); 
    Sitecore.Data.Serialization.ItemReference itemReference = new Sitecore.Data.Serialization.ItemReference(db.Name, itemPath); 
    string path = Sitecore.Data.Serialization.PathUtils.GetFilePath(itemReference.ToString()); 

    Sitecore.Data.Serialization.LoadOptions opt = new Sitecore.Data.Serialization.LoadOptions(db); 
    opt.ForceUpdate = true; 

    using (new Sitecore.SecurityModel.SecurityDisabler()) 
    { 
     Sitecore.Data.Serialization.Manager.LoadItem(path, opt); 
    } 
} 
+0

感謝您的評論,我會看看。 –

+0

我已經使用這兩個帖子的信息,但仍然沒有進展。最終使用ItemReference對象最終給我一樣的:我以前使用的C:\ inetpub \ wwwroot \ sitecoretest \ Data \ serialization \ master \ sitecore \ content \ Home \ Standard-Items \ Teasers \ Our-Clients.item路徑。不過,我仍然看不到SiteCore內的變化,仍然沒有錯誤。所以我有更簡潔的代碼,但沒有解決方案。 –

+0

我測試了所提供的功能,並且能夠恢復項目。步驟:1.序列化你/ sitecore/content/home項目。 2.更改該項目的一個字段。 3.用「/ sitecore/content/home」路徑調用該方法。 –

2

您可以隨時如何Sitecore的代碼工作使用反射,下面的方法被調用當您單擊後端「恢復項目」:

protected virtual Item LoadItem(Item item, LoadOptions options) 
{ 
    Assert.ArgumentNotNull(item, "item"); 
    return Manager.LoadItem(PathUtils.GetFilePath(new ItemReference(item).ToString()), options); 
} 

在LoadOptions,您可以指定是否要覆蓋(「恢復項目」)或只是更新(「更新項目」)。

請參閱Sitecore.Shell.Framework.Commands.Serialization.LoadItemCommand瞭解更多信息。

+0

感謝您的評論,我會看一看。 –

+0

再次感謝,請參閱下面的評論以獲取更多信息。 –

+0

做到以上... –

0

我花了一段時間才能奏效,但是你必須刪除.item恢復樹

當嘗試這個

public void RestoreItemTree(string itemPath) 
    { 
     var db = Factory.GetDatabase("master"); 
     var itemReference = new ItemReference(db.Name, itemPath); 
     var path = PathUtils.GetFilePath(itemReference.ToString()); 

     if (!System.IO.File.Exists(path)) 
     { 
      throw new Exception("File not found " + path); 
     } 

     var opt = new LoadOptions(db); 
     opt.ForceUpdate = true; 

     using (new SecurityDisabler()) 
     { 
      Manager.LoadItem(path, opt); 
      Manager.LoadTree(path.Replace(".item", ""), opt); 
     } 
    }