如何以编程方式恢复儿童Sitecore项目?

时间:2016-02-23 22:46:09

标签: c# sitecore sitecore7

我目前可以通过调用item.DeleteChildren()删除项目,如果有错误,我想通过原始项目列表var originalItems = item.GetChildren();来恢复这些项目但是如何恢复它们以便那些模板字段里面还有吗?

我尝试执行以下操作,但所有操作都是重新创建没有字段值的模板。

foreach (Item backupItem in backupItems)
{
    item.Add(backupItem.Name, backupItem.Template);
}

1 个答案:

答案 0 :(得分:3)

您可以将其归档而不是删除它们,并在需要时将其还原。

http://www.sitecore.net/learn/blogs/technical-blogs/john-west-sitecore-blog/posts/2013/08/archiving-recycling-restoring-and-deleting-items-and-versions-in-the-sitecore-aspnet-cms.aspx

John West的代码

<div class="form-group">
  <h3>id</h3>
  <input ng-maxlength="idMax" type="text" class="form-control" ng-model="id">
</div>
<div class="form-group">
  <h3>Name</h3>
  <input ng-maxlength="nameMax" type="text" class="form-control" ng-model="name">
</div>

要恢复,请使用相同的Archive类。

Sitecore.Data.Items.Item item = Sitecore.Context.Item;
Sitecore.Diagnostics.Assert.IsNotNull(item, "item");
Sitecore.Data.Archiving.Archive archive = 
Sitecore.Data.Archiving.ArchiveManager.GetArchive("archive", item.Database);

foreach (Sitecore.Data.Items.Item child in item.Children)
{
  if (archive != null)
  {
    // archive the item
    archive.ArchiveItem(child);
    // to archive an individual version instead: archive.ArchiveVersion(child);
  }
  else
  {
    // recycle the item
    // no need to check settings and existence of archive
    item.Recycle();
    // to bypass the recycle bin: item.Delete();
    // to recycle an individual version: item.RecycleVersion();
    // to bypass the recycle bin for a version: item.Versions.RemoveVersion();
  }
}