我正在尝试使用sitecore API来序列化和恢复sitecore项目。我已经创建了一个WCF应用程序来检索给定ID或sitecore路径(/sitecore/content/home)的项目名称,检索子项给定id或路径的项目名称列表。我还可以序列化内容树。
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\serialization\master\sitecore\content\Home\Standard-Items\Teasers\Our-Clients
两个人似乎都没有做任何事情。我更改了项目的预告标题,并试图恢复到之前,但每次更改仍然存在。
任何帮助都将不胜感激,因为SiteCore文档非常有限。
发布于 2011-12-05 23:20:46
您有用于强制覆盖(也称为恢复)的正确LoadOptions。
我怀疑您用于.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);
}
}发布于 2011-12-03 15:10:22
你总是可以使用Reflector检查Sitecore代码是如何工作的,当你在后端单击"Revert Item“时,会调用以下方法:
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。
发布于 2015-01-13 11:19:52
我花了一段时间才弄明白,但在恢复树时必须删除.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);
}
}https://stackoverflow.com/questions/8363016
复制相似问题