How to write a query in template's DataSource to generate the path of item?
If I write a query in DataSource field and a page uses the template, the datasource value will be the item's path as dynamic datasource, like the screenshot.
If you are looking for Sitecore to auto-generate a sublayout's datasource to the Item it is placed on, similar to Template Fields having Source property, there is not anything out to box to achieve this currently.
If you are looking to enter a query into the Sublayout's datasource you will need to use the Enable Datasource Query field on the Sublayout Item. Pass in a query via the Datasource:
And then retrieve the query and execute;
protected void Page_Load(object sender, EventArgs e)
{
//Handle a single GUID
var searches = ((Sublayout)this.Parent).DataSource;
if (searches.IsGuid())
{
var itemDummyList = new List<Item>();
itemDummyList.Add(Sitecore.Context.Database.GetItem(searches));
this.SampleListView.DataSource = itemDummyList;
this.SampleListView.DataBind();
return;
}
//Handle a search query
using (var context = ContentSearchManager.CreateSearchContext((SitecoreIndexableItem)Sitecore.Context.Item))
{
var timer = new Stopwatch();
timer.Start();
//This gives us our IQueryable
var query = LinqHelper.CreateQuery(context, UIFilterHelpers.ParseDatasourceString(searches))
.Select(toItem => toItem.GetItem()).Take(10);
this.SampleListView.DataSource = query;
this.SampleListView.DataBind();
timer.Stop();
//Display the query time only in Debug Mode
if (Sitecore.Context.PageMode.IsDebugging)
{
this.RunTime.Text = " Debug Information: " + timer.ElapsedMilliseconds + " ms to render";
}
}
}
Reference John West; blog on Sitecore 7 Datasource Explained