Override Sitecore Layout Details

Sitecore allows developers to extend most of its features. Last time, we experienced an issue whereby webmasters were making mistakes when configuring contents on the Homepage of the site.

The caching of the sitecore sublayouts were not set properly or were missed during configuration of contents via the Presentation Details.

caching

In order to prevent this issue from occurring, I had to override the Sitecore Layout Details.xml. At first, it was very difficult to find the xml. One thing to note is that all the different UI in Sitecore are stored in the sitecore folder found in the Website.

Overriding the Sitecore Layout Details

First is to find the LayoutDetails.xml. The file is found in the following path: Website\sitecore\shell\Applications\Content Manager\Dialogs\LayoutDetails.

Copy the LayoutDetails.xml and paste it in the override folder: Website\sitecore\shell\Override\Applications\Content Manager\Dialogs.

You will see that when opening the Override folder, there is no Application folder and so on. As a best practice, I have use the same directory structure to paste the LayoutDetails.xml.

Once this is completed, open the xml using nodepad++ or any text editor. You will see the <CodeBeside> Tag. This contains the Method and Assembly name that will be triggered upon opening the Layout Details in sitecore.

Now, the interesting part. We needs to implement a method that will set the caching. In my case, it is when the webmaster clicks on the OK button on the Layout Details that the code will trigger.

Implementation Logic

  1. Get the current item.
  2. Get the layout field using the Sitecore Fields.
  3. Loop into the device definition – Retrieve each device renderings.
  4. Set the caching values.
  5. Set the modified layout to the item.

protected override void OnOK(object sender, EventArgs args)
{
   using (new SecurityDisabler())
   {
      var currentItem = this.GetCurrentItem();

      var layout = this.Layout;

      var layoutField = new LayoutField(currentItem.Fields[Sitecore.FieldIDs.LayoutField]);

      var layoutDefinition = LayoutDefinition.Parse(layout);

      if (!string.IsNullOrWhiteSpace(layout))
      {
         layoutDefinition.LoadXml(layout);

         foreach (DeviceDefinition device in layoutDefinition.Devices)
         {
            var currentRenderings = this.GetRenderingList(device.ID, layoutDefinition);

            foreach (RenderingDefinition rendering in currentRenderings)
            {
                rendering.Cachable = "1";
                rendering.VaryByData = "1";
                rendering.VaryByParameters = "0";
                rendering.VaryByQueryString ="0";
                rendering.VaryByUser = "1";
             }
          }

          currentItem.Editing.BeginEdit();
          {
             layoutField.Value = layoutDefinition.ToXml();
          }

          currentItem.Editing.EndEdit();
      }

      SheerResponse.CloseWindow();
    }
}

private Item GetCurrentItem()
{
   var id = WebUtil.GetQueryString("id");

   var language = Language.Parse(WebUtil.GetQueryString("la"));

   var version = Sitecore.Data.Version.Parse(WebUtil.GetQueryString("vs"));

   return Factory.GetDatabase("master").Items.GetItem(id, language, version);
}

private ArrayList GetRenderingList(string deviceId, LayoutDefinition layoutDefinition)
{
   return layoutDefinition.GetDevice(deviceId).Renderings;
}

Based on your requirements, you can specify what renderings you need to cache. You can make use of a configuration file which will store the different rendering Ids.

Advertisements

One thought on “Override Sitecore Layout Details

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s