June 22, 2012

Sublayout Queryable Datasource module on trac

I have release a new module on trac. This one allow you to select the datasource dynamically with a XPath query in the dialog open when you insert a sublayout in webedit mode.
If you don't like the video version here is a small description of this module.

Purpose

If you have a multisite solution and if you want to use the field "Datasource Location" and "Datasource template" on you sublayout to open the datasource selection dialog when you add this sublayout on your page in page edit mode, you are not able to select the folder dynamically. by default sitecore only allow to select a folder like /sitecore/content/MyDatas. The idea of this module is to enable the possibility to select this folder relatively to the current item with an XPath query as you do in the fields for example. After installing this module you will be able to select this folder with a query syntaxt like this one for example:
query:ancestor-or-self::*[@@templatename = 'Website Root']/Data/Demo

How to Install?

  • You can download package from here.
  • Install it with the sitecore installation tool in the desktop of sitecore

Next steps

If you have any issues or suggestions for the Sublayout Queryable Datasource module, please report it by posting a comment on this post.

June 18, 2012

Datasources queryable

[EDIT] I have release a new module on trac to do that so it will be easier to use this module. For more information take a look at this post: http://sitecoreblog.blogspot.be/2012/06/sublayout-queryable-datasource-module.html[/EDIT]


In the video from Nick Wesselman at the sitecore virtual user group, he show how to use a subitem as a datasource for a sublayout.

The datasource is used when you insert a sublayout in webedit mode to display a popup to select an create if needed the datasource of this sublayout.


The problem happend when you have a multisite solution for example because with the system from sitecore you cannot set a relative datasource. The idea was to enable the possbility to set this datasource using a query as in the fields.

To code for this is very simple:
public class SublayoutQueryableDatasource
{
    public void Process(GetRenderingDatasourceArgs args) 
    {
        Assert.IsNotNull(args, "args");

        string text = args.RenderingItem["Datasource Location"]; //Get the value from the field
        if (!string.IsNullOrEmpty(text))
        {
            if (text.StartsWith("query:") && !string.IsNullOrEmpty(args.ContextItemPath))
            {
                var contextItem = args.ContentDatabase.GetItem(args.ContextItemPath);

                if (contextItem != null)
                {
                    text = text.Remove(0, 6); //remove the query:
                    var item = contextItem.Axes.SelectSingleItem(text); //Execute the query

                    if (item != null)
                    {
                        args.DatasourceRoots.Add(item);
                    }
                }
            }
        }
    }
}

After that you just have to included this pipeline in the correct place by adding this into a file in your /app_Config/Include folder:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <getRenderingDatasource>
        <processor type="YOURNAMESPACE.SublayoutQueryableDatasource, YOURNAMESPACE"
                   patch:before="processor[@type='Sitecore.Pipelines.GetRenderingDatasource.GetDatasourceLocation, Sitecore.Kernel']"/>
      </getRenderingDatasource>
    </pipelines>
  </sitecore>
</configuration>

After that you are able to set the datasource like this: