3

When I click in the Sitecore client to change versions, I would like to be able to run some code. So, is there any events trigger when I select any version number in the Sitecore client?

I am sharing screenshot from where I am selecting version. I need to get their value to be able to run some code.

enter image description here

0
4

To add on @Chris Auer answer, you may find below the code snippet showing where you should perform the change.

You need to override the OnLoad method of the GalleryVersionsForm class from the namespace Sitecore.Shell.Applications.ContentManager.Galleries.Versions in the assembly Sitecore.Client.dll.

public class GalleryVersionsExtensionForm : GalleryVersionsForm
{
    protected override void OnLoad(EventArgs e)
    {
        Assert.ArgumentNotNull((object)e, nameof(e));
        base.OnLoad(e);
        if (Context.ClientPage.IsEvent)
        {
            //APPLY IMPLEMENTATION HERE
            //Code Snippet to retrieve selected version
            var formParameters = Context.ClientPage.Request.Form["__PARAMETERS"];

            var selectedVersion = formParameters.Split(',')[2].Replace(")", string.Empty).Split('=')[1];

            return;
        }
        Item currentItem = GalleryVersionsForm.GetCurrentItem();
        if (currentItem != null)
        {
            if (currentItem.IsFallback)
            {
                HtmlGenericControl htmlGenericControl = new HtmlGenericControl("div");
                htmlGenericControl.InnerText = Translate.Text("No version exists in the current language. You see a fallback version from '{0}' language.", (object)currentItem.OriginalLanguage);
                htmlGenericControl.Attributes["class"] = "versionNumSelected";
                Context.ClientPage.AddControl((System.Web.UI.Control)this.Versions, (System.Web.UI.Control)htmlGenericControl);
            }
            else
            {
                Item[] versions = currentItem.Versions.GetVersions();
                for (int index = versions.Length - 1; index >= 0; --index)
                {
                    Item obj = versions[index];
                    XmlControl control = ControlFactory.GetControl("Gallery.Versions.Option") as XmlControl;
                    Assert.IsNotNull((object)control, typeof(XmlControl), "Xml Control \"{0}\" not found", "Gallery.Versions.Option");
                    Context.ClientPage.AddControl((System.Web.UI.Control)this.Versions, (System.Web.UI.Control)control);
                    CultureInfo culture = Context.User.Profile.Culture;
                    string str1 = obj.Statistics.Updated == DateTime.MinValue ? Translate.Text("[Not set]") : DateUtil.FormatShortDateTime(DateUtil.ToServerTime(obj.Statistics.Updated), culture);
                    string str2 = obj.Statistics.UpdatedBy.Length == 0 ? "-" : obj.Statistics.UpdatedBy;
                    string str3 = obj.Version.ToString() + ".";
                    string str4 = obj.Version.Number != currentItem.Version.Number ? "<div class=\"versionNum\">" + str3 + "</div>" : "<div class=\"versionNumSelected\">" + str3 + "</div>";
                    control["Number"] = (object)str4;
                    control["Header"] = (object)Translate.Text("Modified <b>{0}</b> by <b>{1}</b>.", (object)str1, (object)str2);
                    control["Click"] = (object)string.Format("item:load(id={0},language={1},version={2})", (object)currentItem.ID, (object)currentItem.Language, (object)obj.Version.Number);
                }
            }
        }
        Item obj1 = Sitecore.Client.CoreDatabase.GetItem("/sitecore/content/Applications/Content Editor/Menues/Versions");
        if (obj1 == null || !obj1.HasChildren)
            return;
        string queryString = WebUtil.GetQueryString("id");
        this.Options.AddFromDataSource(obj1, queryString, new CommandContext(currentItem));
    }
}

It is in the IF STATEMENT of the IsEvent that you need to add your implementation. The statement is true when the user clicks on a version.

Note that you may also add your custom implementation, without overriding the whole class, after the base method call. Please see below.

public class GalleryVersionsExtensionForm : GalleryVersionsForm
{
    protected override void OnLoad(EventArgs e)
    {
        Assert.ArgumentNotNull((object)e, nameof(e));
        
        base.OnLoad(e);
        
        if (Context.ClientPage.IsEvent)
        {
            //APPLY IMPLEMENTATION HERE
            var formParameters = Context.ClientPage.Request.Form["__PARAMETERS"];

            var selectedVersion = formParameters.Split(',')[2].Replace(")", string.Empty).Split('=')[1];
        }
    }
}

Once you have your code, you need to change the codebeside of the the xml for the version gallery UI.

From

<CodeBeside Type="Sitecore.Shell.Applications.ContentManager.Galleries.Versions.GalleryVersionsForm,Sitecore.Client"/>

To

<CodeBeside Type="YourNamespace,YourAssemblyName"/>

IMPORTANT

When changing the version gallery UI, it will apply on both the ribbon and the on item version list. Please see screenshot below where the red squares highlight the affected sections

enter image description here

UPDATE 1

The above does not work on the Experience Editor. From a brief analysis, you will need to update/override the JavaScript file SelectVersionGallery.js to add your custom implementation to the event listener selectversionoption:click. The file can be found at the path sitecore\shell\client\Sitecore\Speak\Ribbon\Galleries\Versions\SelectVersionGallery.js

UPDATE 2

To retrieve the selected version, the only way is to read the __PARAMETERS. The returned value is

"item:load(id={110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9},language=en,version=1)"

So, you'll need to perform some replace and split in order to have the version number

var formParameters = Context.ClientPage.Request.Form["__PARAMETERS"];

var selectedVersion = formParameters.Split(',')[2].Replace(")", string.Empty).Split('=')[1];

GetCurrentItem method implementation

private static Item GetCurrentItem()
{
    string queryString1 = WebUtil.GetQueryString("db");
    string queryString2 = WebUtil.GetQueryString("id");
    Language index1 = Language.Parse(WebUtil.GetQueryString("la"));
    Sitecore.Data.Version index2 = Sitecore.Data.Version.Parse(WebUtil.GetQueryString("vs"));
    Database database = Factory.GetDatabase(queryString1);
    Assert.IsNotNull((object)database, queryString1);
    return database.Items[queryString2, index1, index2];
}
9
  • Can we achieve this functionality through experience editor ?
    – Sonu
    Jul 9 '20 at 18:13
  • Please see update section Jul 9 '20 at 18:52
  • How to get a clicked version details through this code as you mentioned "Item currentItem = GalleryVersionsForm.GetCurrentItem();" but it is throwing an error. I resolved this issue by creating method but i am not able to fetch clicked version details instead of previous version details.
    – Sonu
    Jul 9 '20 at 18:57
  • Please see update 2 section. I have also updated the above code to where to use it and also the implementation of the GetCurrentItem Jul 10 '20 at 11:50
  • Okay ,@Hishaam Can we get version details through Experience editor with any event without javascript?
    – Sonu
    Jul 10 '20 at 12:22
2

The best I can see if to override the code beside for \sitecore\shell\Applications\Content Manager\Galleries\Versions\Gallery Versions.xml. It is currently set to Sitecore.Shell.Applications.ContentManager.Galleries.Versions.GalleryVersionsForm. This would allow you to insert some code as the next version gets loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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