Clone contentitem version

Topics: Customizing Orchard, General, Nederlands (Dutch), Writing modules
Aug 23, 2013 at 5:25 PM
I am building a version manager module (a modification on the iroo.versionmanager) and want to create a new version of an existing version of a content item. I have the following code, but it changes the version of the content item to a new version and does not clone the version of the content to new version of the content.
int lastversionNumber = _contentManager.GetAllVersions(id).Max(r => r.VersionRecord.Number);

ContentItem contentItem = _contentManager.Get(id, VersionOptions.VersionRecord(versionId));
_contentManager.Create(contentItem, VersionOptions.Number(lastversionNumber + 1));
Does anybody know how to clone a specific version of a content item to a new version?
Aug 23, 2013 at 6:44 PM
Did you look at the new clone feature that is in 1.7 (contributed by Zoltan)?
Aug 23, 2013 at 6:53 PM
That's clones a content item to a new content item. I just want to clone a (history) version of a content item. I want to give a content manager the option to get a previous version of a content item and continue with that version.
Aug 23, 2013 at 7:04 PM
Ah, sorry. I'm afraid you'll have to dive into the default content manager's implementation to find out.
Aug 23, 2013 at 9:01 PM
I took a dive into the Orchard.ContentManagement.DefaultContentManager, and I think it is a bug. I tried to fix it so see if I make it run. Now I can clone the version part, but I loose the connection with the other parts.

This is a part of the create method with the changed code. Hope somebody can see my mistake.
public virtual void Create(ContentItem contentItem, VersionOptions options) {
    if (contentItem.VersionRecord == null) {
        // produce root record to determine the model id
        contentItem.VersionRecord = new ContentItemVersionRecord {
            ContentItemRecord = new ContentItemRecord {
            Number = 1,
            Latest = true,
            Published = true

    if (options.VersionNumber != 0)
    /////added code
        ContentItemVersionRecord newVersion = new ContentItemVersionRecord
            ContentItemRecord = contentItem.VersionRecord.ContentItemRecord,
            Data = contentItem.VersionRecord.Data,
            Latest =
                contentItem.VersionRecord.ContentItemRecord.Versions.Max(r => r.Number) <= options.VersionNumber,
            Number = options.VersionNumber,
            Published = options.IsPublished
        contentItem.VersionRecord = newVersion;
     /////orginal code
        // add to the collection manually for the created case

////commented orginal code, it referenced the old version
    // version may be specified
    //if (options.VersionNumber != 0) {
    //    contentItem.VersionRecord.Number = options.VersionNumber;

    // draft flag on create is required for explicitly-published content items
    if (options.IsDraft) {
        contentItem.VersionRecord.Published = false;

Aug 23, 2013 at 11:46 PM
If you think it's a bug, and you have repro steps, please file a bug.
Aug 24, 2013 at 7:27 AM
I am not sure if it is a bug or I use the content manager in a wrong way. I have filed it as an issue, issue 20044.
Nov 15, 2013 at 5:04 PM
Edited Nov 15, 2013 at 5:06 PM
Use GetDraftRequired function
//GetDraftRequired(this IContentManager manager, int id)
 contentItem = _contentManager.GetDraftRequired(id) 
 contentItem = _contentManager.Get(id, VersionOptions.DraftRequired)
In Orchard.ContentManagement.VersionOptions you can see
/// <summary>
/// Gets the latest version and creates a new version draft based on it.
/// </summary>
public static VersionOptions DraftRequired { ... }
About the "draft" see here
Nov 15, 2013 at 5:29 PM
Thank you for your response! It creates a new version record, but can I clone an old version of the content item and not the current version?
Nov 29, 2013 at 11:47 AM
EdwardK wrote:
... can I clone an old version of the content item and not the current version?
I found only such a solution:
1) Save the last published record
2) Publish an old record that you want to clone
3) Clone
4) Publish the saved record to restore the state that was before cloning

Something like that:
ContentItem  lastPublishedItem = _contentManager.Get(id);

ContentItem  clonedItem =_contentManager.Get(id, VersionOptions.VersionRecord(versionId));

ContentItem  clone= _contentManager.GetDraftRequired(id);

Nov 29, 2013 at 2:28 PM
Hi, Thank you for thinking! I have tried your solution. When I publish a previous version and do a GetDraftRequired I get the latest version. The contentmanager does not create a new version.