是否有允许我复制或复制的API以及只获得单个版本的项目?
Item.Duplicate()
和Item.CopyTo()
都会获得所有编号的版本和语言。
我在Sitecore 6.3.1上。
答案 0 :(得分:3)
item.Versions.RemoveAll(true);
也会移除最新版本。
您可以复制或复制该项目(包含其所有版本),然后尝试以下内容:
private void RemovePreviousVersions(Item myItem, bool includeAllLanguages)
{
// get the most recent version
Item currentVersion = myItem.Versions.GetLatestVersion();
Item[] versions = myItem.Versions.GetVersions(includeAllLanguages);
// loop through the item versions
foreach(Item itemVersion in versions)
{
// remove the version if it is not the most recent
if (!itemVersion.Version.Number.Equals(currentVersion.Version.Number))
{
itemVersion.Versions.RemoveVersion();
}
}
}
答案 1 :(得分:2)
我不必复制项目,您可以在树中插入新项目,但取决于您的确切要求。
http://briancaos.wordpress.com/2011/01/14/create-and-publish-items-in-sitecore/
您是否尝试使用特定语言获取最新版本的项目并重复该项?
Item masterItem = master.GetItem("/sitecore/content/home").Versions.GetLatestVersion(Sitecore.Context.Language);
masterItem.Duplicate("newItem");
或复制/复制,然后删除新创建项目的版本:
item.Versions.RemoveAll(true);
但你需要检查它是否也删除了最新版本。
答案 2 :(得分:0)
我使用此代码仅复制一个项目的最新版本,包括儿童。它会创建一个新项目,创建必要的版本并设置字段。
private Item CopyLatestVersion(Item target, Item itemToCopy, string copyOfName)
{
var copiedItem = target.Add(copyOfName, new TemplateID(itemToCopy.TemplateID));
var db = itemToCopy.Database;
bool sharedFieldsPopulated = false;
foreach (var lang in itemToCopy.Languages)
{
var languageVersionToCopy =
db.GetItem(itemToCopy.ID, lang, Sitecore.Data.Version.Latest);
if (languageVersionToCopy.Versions.Count == 0)
{
continue;
}
var languageVersionCopied = db.GetItem(copiedItem.ID, lang);
//we have to check if there is already version cause when the item was created
//Sitecore automatically creates a version already with the context language
if (languageVersionCopied.Versions.Count == 0)
{
languageVersionCopied = languageVersionCopied.Versions.AddVersion();
}
languageVersionCopied.Editing.BeginEdit();
foreach (Sitecore.Data.Fields.Field field in languageVersionToCopy.Fields)
{
if (field.HasValue
&& (!sharedFieldsPopulated || !field.Shared)
&& field.Name.Trim() != "")
{
languageVersionCopied.Fields[field.Name].Value = field.Value;
}
}
//this doesn't update the edited by field, which we don't want to set to current user
//but keep the original updater like in default copy to functionality
languageVersionCopied.Editing.EndEdit(false, false);
//during the first iteration we also copy the shared fields afterwards we don't
sharedFieldsPopulated = true;
}
//recursively copying also all children
foreach (Item child in itemToCopy.Children)
{
CopyLatestVersion(copiedItem, child, child.Name);
}
return copiedItem;
}