2

是否有一个 API 可以让我复制或复制并且项目只是获得一个版本?
Item.Duplicate()并且Item.CopyTo()都获得所有编号的版本和语言。

我在 Sitecore 6.3.1 上。

4

3 回答 3

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();
            }
        }
    }
于 2012-12-07T11:01:07.507 回答
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);

但是您需要检查它是否也删除了最新版本。

于 2012-12-06T23:08:18.317 回答
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;
    }
于 2015-07-02T12:43:06.090 回答