Add version including Final Layout from selected language

SK
Sitecore Technology MVP & Senior Director​
  • Twitter
  • LinkedIn

Add version including Final Layout from selected language

Sitecore 8 comes loaded with some new features. One of which being versioned layouts.

When you add a rendering through the Experience Editor, it is always updates the __Final Renderings field in the latest version for the current context language, thus making it versionable.

Users should be aware that a rendering added in English language won't show up in Spanish. The editors at one of our client(s) who rely completely on Experience Editor asked us to change this behavior. This article outlines the approach I have taken in order to address this so that they don't have to repeat this action of designing the page in every language.

I have used JetBrains dotPeek in order to see what exactly was happening when the user Adds a new version.

I added a custom functionality which will give an option to the user to create an item version by copying the Final Layout from selected language.

Login to Sitecore and switch to the core database and add a new button to the Versions ribbon located at /sitecore/content/Applications/WebEdit/Ribbons/WebEdit/Versions and create a new chunk (in this example, I named it as Custom) and add a Large Button named Add Version with Final Layout inside it_._

Using Sitecore Rocks, navigate to these items in the Sitecore Explorer and

  • Design the Layout for the chunk by adding a new Chunk Rendering.
  • Design the Layout for the button by adding a LargeButton Rendering. This rendering has a click event which invokes a script file.
  • Save your changes.

Now, we have to create a new AddItemVersionWithFinalLayout.js and add logic which will open a Modal Dialog.

Create a new xmlcontrol AddVersionWithFinalLayout.xml and a supporting code behind class AddFinalLayoutDialogForm.cs class which inherits the Sitecore.Web.UI.Pages.DialogForm.

Create two classes as defined in the script file and add the references to these classes in a config file.


  
 









The following is a code snippet from the Dialog Form which show how I create the item version and set the __Final Renderings field value.

        protected override void OnLoad(EventArgs e)
        {
             ...
        }

        protected override void OnOK(object sender, EventArgs args)
        {
            ...

            //this dialog value is needed for the experience editor to complete the SPEAK request
            SheerResponse.SetDialogValue(UpdateVersionWithFinalLayout().ToString());
            OnCancel(sender, args);
        }
        private bool UpdateVersionWithFinalLayout()
        {
             ...
             var latestVersion = contextItemInSelectedLanguage.Versions.GetLatestVersion();

             using (new SecurityDisabler())
             {
                 contextItem.Versions.AddVersion();
                 contextItem.Editing.BeginEdit();
                 var latestVersionFinalLayout = LayoutField.GetFieldValue(latestVersion.Fields[FieldIDs.FinalLayoutField]);
                 //set the value of the final layout from user selected language
                 LayoutField.SetFieldValue(contextItem.Fields[FieldIDs.FinalLayoutField], latestVersionFinalLayout);
                 contextItem.Editing.EndEdit();

                   ...

                 return true;
             }
         }
         return false;
     }

This new feature is working well as per the requirement. Hoping that the information provided here is helpful.

This patch has been applied and tested on Sitecore 8.1 Update-3. Please clear your browser caches before testing this feature.

As always, please test these changes on your development environment before applying it on Production.

Related Blogs

Latest Blogs