0

I want to add new version when I publish any item in sitecore.

Is there any option available in sitecore?

If not then how we can achieve this?

4
  • 1
    You can add version to item when the user is updating the item. You can do this by using workflows. So, if the version is not in the Approve state, it won't be published. Commented Jan 4, 2019 at 10:37
  • I don't want to add version manually.It should be automatic when anyone publish anything Commented Jan 4, 2019 at 10:51
  • Can you explain why you want to do this? As @Trayvek's answer below describes, this has the potential to create a ton of unnecessary data. Commented Jan 4, 2019 at 15:09
  • 1
    I've just updated my answer a little as I neglected to even mention performance. It would be great to hear why you need to have a new version when someone publishes, because I would really be careful going down this route. There might be a better way to accomplish what you're trying to achieve.
    – Trayek
    Commented Jan 7, 2019 at 10:51

3 Answers 3

7

While this is technically possible (by hooking into the publish:end event), I think this might have some unexpected implications. Let me run you through a couple of examples.

With workflow disabled:
Let's say I have the Home item on version 1. I publish it, and hook into the publish:end event. This will now create version 2.
I'm not going to change anything so version 1 and 2 are the same.
Now someone kicks off a sitewide publish. The home item will be included, because on web database I have version 1 while master database has a newer version. The publish:end event will run again, and now create a version 3.

You can imagine that this will create many many (unused and unnecessary) versions - and this example was without making any edits. If we take the above example, but I'm starting to edit version 2 and someone publishes, that will now create version 3. If I'm not aware of this and I keep editing, I will be editing an older version, and my work will not actually show up on the website.

With workflow enabled:
Again, home item is in version 1. I publish which creates version 2. This version is not in an approved state in the workflow. As publishes go on, multiple versions get created, flooding the workbox with items to be approved (and that's not even going into what I've explained above where I might have newer content in older versions).

I'd recommend you create some type of process if you want to go this route, or (preferably) use Sitecore's Workflow engine. Don't forget that if you have many versions of an item, it is likely to impact performance!

Having said all that, this is how you hook into an event:

First off, you'll need to create your code:

public void MyMethod(object sender, EventArgs args)
{
  var sitecoreArgs = args as Sitecore.Events.SitecoreEventArgs;
  if (sitecoreArgs == null)
    return;
  var publisher = sitecoreArgs.Parameters[0] as Publisher;
    return;

  var rootItem = publisher.Options.RootItem;
  rootItem.Versions.AddVersion()
}

Then you need to hook this into the publish:end event. You can use Sitecore's patching mechanism for this. The contents of your patch file would look something like this:

<sitecore>
  <events>
    <event name="publish:end">
      <handler type="MyNamespace, MyDll" method="MyMethod"/>
    </event>
  </events>
</sitecore>

Don't forget that the above is overly simplistic. Especially if you have a multi-lingual site the complexity of this increases very rapidly.

1

When a user creates an item, Sitecore creates a version in the current content language and initiates the workflow for that version. After that item completes its workflow, if a user edits the item in that language, Sitecore creates a new version and initiates its workflow. In some cases when working as a Sitecore administrator, Sitecore does not create new versions or initiate workflows. So the right way to do it is to assign workflows to your template.

Have a look here: https://community.sitecore.net/technical_blogs/b/sitecorejohn_blog/posts/thoughts-on-versioning-items-with-the-sitecore-asp-net-cms

1
  • I want to create new version when anyone publish any item. Commented Jan 4, 2019 at 10:46
0

When an Editor edit the content item, new version gets created in that particular language. Once editing is complete and the content item is approved through the workflow process, it gets published on the live.

Second, When an Editor creates a new item, Sitecore creates a new version in draft workflow state.

Third, You can customize the publish behaviour through pipeline events and add your custom publishing logic there.

Event Configuration:

<event name="publish:begin" >
    <handler type="Sitecore.Custom.Pipelines.CustomPublish, WebSite" method="OnPublish"/>
</event>

Event Code:

namespace Sitecore.Custom.Pipelines
    {
        public class CustomPublish
        {            
            public void OnPublish(object sender, EventArgs args) 
            {
                // Your custom logic goes here
            }
        }
    }

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.