Applying datasource to sublayouts on Sitecore items

In Sitecore you can apply a datasource to a sublayout when adding it to a template/item. But the datasource is not automatically used. Your context will not change to the item specified in the datasource.
The datasource is therefore not used as a context change – but it can be. All you need is to use the brilliant code provided by Alexey Rusakov and alter it a bit.

Here is my thoughts: I apply a sublayout to a page. I then alter the datasource because I want the sublayout to get it’s data from another item than the current one.
First I have rewritten the code from Alexey into a function that will return the RenderingDefinition from a specific sublayout on the current item for the current context. All you need is the path to the sublayout in question:

using Sitecore.Layouts;
using Sitecore.Data.Items;

private RenderingDefinition GetRenderingDefinition(string path)
  // Get the Layout definition from the current item
  string rend = Sitecore.Context.Item.Fields["__renderings"].Value;
  LayoutDefinition layout = LayoutDefinition.Parse(rend);
  // Get the current device definition
  DeviceDefinition device = layout.GetDevice(Sitecore.Context.Device.ID.ToString());
  // Get the sublayout to find
  Item mySublayout = Sitecore.Context.Database.Items[path];
  // Get the definition for the sublayout
  RenderingDefinition rendering = device.GetRendering(mySublayout.ID.ToString());
  return rendering;

I can then use the RenderingDefinition to get the item that the datasource is pointing to. If there is no datasource it means that no datasource have been applied and I’ll use the current item:

private Item GetCurrentItem()
  RenderingDefinition def = GetRenderingDefinition("/sitecore/layout/Sublayouts/MySubLayout");
  if (def.Datasource == String.Empty)
    return Sitecore.Context.Item;
  Item dataSourceItem = Sitecore.Context.Database.GetItem(def.Datasource);
  Sitecore.Diagnostics.Assert.IsNotNull(dataSourceItem, "Could not find datasource item at " + def.Datasource);
  return dataSourceItem;

In my codebehind for the sublayout I use the item returned by the GetCurrentItem() function instead of the Sitecore.Context.Item, and voila! I have my context change.

BTW: The RenderingDefinition contains much more than the datasource. You can also get the parameters, placeholder and caching informations.
Subnote: Alexey Rusakov describes on his blog that in Sitecore 6 you can use the Attributes[“sc_parameters”] to get the parameters for a sublayout. That’s true, but the parameters does not contain the datasource, only the parameters.

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in Sitecore 5, Sitecore 6 and tagged , , , . Bookmark the permalink.

11 Responses to Applying datasource to sublayouts on Sitecore items

  1. Paul says:

    Great article! This has been very helpful for me, so thank you. The issue I’m encountering with this code however is when I attempt to place multiple instances of the same SubLayout on a page, each with their own unique Datasource definitions. I can’t get each instance of the SubLayout to recognize its own unique Datasource. It seems like it should be simple to fix this, but I’m stuck!

  2. misteraidan says:

    I’m still very surprised that custom parameters are easy to get to, but something as important as DataSource needs such a round-about method for accessing one little string!!

    And if it doesn’t work with a Sublayout/UserControl, why didn’t Sitecore just provide a base method with a couple of properties that would work with the data from the Sitecore item. Would save us all this code!

    Oh well.. sorry to rant on your blog dude.. the code is good.. we’re now using it ourselves :-)

  3. misteraidan says:

    “why didn’t Sitecore just provide a base method”

    revision: base class :-)

  4. Pingback: Get the Sitecore Sublayout from your .aspx page (Usercontrol) « Brian Pedersen’s Sitecore and .NET Blog

  5. Pingback: Get the Sitecore Sublayout from your .aspx page (Usercontrol) |

  6. Kevin Williams says:

    Am I right that this will not work if you use the same sublayout multiple times on a single content item?

    For example, let’s say I have a general purpose sidebar sublayout that renders a small preview of a content item along with a “read more” link… A page designer might place two or three of these into a given page’s sidebar placeholder – all pointing to different datasources.

    But this code would always return the datasource of the first of these sublayouts on the page, wouldn’t it?

    Sorry for replying to such an old post. :)

  7. Pingback: Create components for Sitecore Page Editor | Brian Pedersen's Sitecore and .NET Blog

  8. Pingback: Sitecore Widgets | Learn and share

  9. Michael Staudenmann says:

    Hi Everyone,
    The solution above get the datasource from the layout item itself and not from the current sublayout (User control). To get datasource and parameters from the different sublayout set in the item, you can use the method suggested here :
    Hope it will help :)


  10. Pingback: Sitecore 6: Architecting Widgets Part 5 - nonlinear thinking

  11. Pingback: Sitecore Widgets

Leave a Reply

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

You are commenting using your 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