This post has been migrated from, we apologise if some of the images or content is missing

This post has been migrated, original date 01 Oct 2008 This article applies to Sitecore 5.3. Ok close on the heels of my previous post comes another useful extension method (I might be getting carried away with these). This extension method is to help those of you developing in Sitecore. I came up against the problem today of needing to extract the parameters associated to sub-layout. For those unclear on parameters and sub-layout these can be found by going to a template in Sitecore (I am assuming Sitecore 5.3), click on the Presentation tab and then click Layout. Click the Edit button beneath a layout with a sub-layout. Click on the Sub-layout and in the new popup select the parameters tag. You should see the following screen: Parameters are useful because they allow you to pass in specific values to a layout.Ok so the code I originally looked at was taken from that given by Sitecore:
25             Database db = Sitecore.Context.Database; 26             Item item = db.Items["/sitecore/templates/SC Printers/Section"]; 27             string rend = item.Fields["__renderings"].Value; 28             LayoutDefinition layout = LayoutDefinition.Parse(rend); 29             DeviceItem dev = Sitecore.Context.Device; 30             DeviceDefinition device = layout.GetDevice(dev.ID.ToString()); 31             Item mySublayout = db.Items["/sitecore/layout/Sublayouts/Two columns"]; 32             RenderingDefinition rendering = device.GetRendering(mySublayout.ID.ToString()); 33             Response.Write("Parameters: " + rendering.Parameters); 34             Response.Write("<br/>");
35             Response.Write("DataSource: " + rendering.Datasource); However there are several problems with this code. It assumes that the item has a rendering specified and not inherited from a template and it will also fail if the device that Sitecore picks up has not associated layout; for example if I specify a device for Firefox users but then don't specify and layouts for it it shoud fall back to the default device, but this code doesn't. Another problem with the code is that it returns the list of parameters as a string, if you have more than one parameter they are all concantinated into one long string. I wanted to seperate these out so that they were more usable. The solution is the following code:
21         private static readonly string _renderingField = "__renderings"; 22 23         public static KeyValuePair<string,string> [] GetSubLayoutParameters(this Item item, Guid subLayoutId) 24         { 25 26             string rend = item.Fields[_renderingField].Value; 27             //if there are no renderings on the item use the template render 28             if (rend.IsNullOrEmpty()) 29             { 30                 rend = item.Fields[_renderingField].InheritedValue; 31             } 32             LayoutDefinition layout = LayoutDefinition.Parse(rend); 33             DeviceItem dev = Sitecore.Context.Device; 34             DeviceDefinition device; 35 36             //get the current device 37             device = layout.GetDevice(dev.ID.ToString()); 38             //if the current device has no layout fail back 39             if (device.Layout == null) 40             { 41                 dev = dev.FallbackDevice; 42                 device = layout.GetDevice(dev.ID.ToString()); 43             } 44 45             //get the rendering for the device 46             RenderingDefinition rendering = device.GetRendering(new ID(subLayoutId).ToString()); 47 48             //create a list of key values 49             List<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>(); 50             string [] pairs = rendering.Parameters.Split('&'); 51 52             foreach(string pair in pairs){ 53                 if(!pair.IsNullOrEmpty()){ 54                     string [] vals = pair.Split('='); 55                     if(vals.Count() == 2){ 56                         parameters.Add(new KeyValuePair<string,string>(vals[0], vals[1])); 57                     } 58                 } 59             } 60 61             return parameters.ToArray(); 62
63         } You will notice that I have used a couple of the extension methods from my Extension Method - Part 1 post. I hope this makes getting parameters a little easier for everyone out there. Further reading: How to find the currently executing sublayout in Sitecore
comments powered by Disqus