Teeknow
Teeknow

Reputation: 1025

Sitecore 8 MVC Glass Query Datasource

It seems like query datasources (ex: query:...) are not supported in Sitecore 8 MVC while using glass. I want to make a relative path so that branch templates will point to the correct datasource when created. Is there a way to do this with Sitecore's query builder? I saw the custom query option where a path can be specified but couldn't seem to get anything going. I think I might add a pipeline processor before the model gets bound by glass to change a datasource that starts with query: into a resolved path and pass that along the pipeline arguments.

Upvotes: 2

Views: 904

Answers (1)

Jonathan Robbins
Jonathan Robbins

Reputation: 2047

You can add a datasource query to a Sublayout or Rendering Datasource Layout field. You will need to add a new Processor to the getRenderingDatasource Pipeline. I myself have used this on Branch Templates to make relative paths to the correct datasource.

Your query:. needs to be defined in the Datasource Location of the sublayout/rendering and make use of ancestor-or-self to make relative paths and traverse the tree to find the parent item holding the datasources.

query:.ancestor-or-self:: *[@@templatename = 'home']/*[@@templatename = 'storage']/*[@@templatename = 'articles']

The processor will need to use the GetRenderingDatasourceArgs. These arguements will provide you pretty much everything you need. Essentially you will need to get the query:. you wrote in the Datasource Locations.

args.RenderingItem["Datasource Location"];

Replace the beginning of the query with the Context Item path (so that its relative) and then make a call to get the items;

private IEnumerable<Item> GetDatasourceLocationsFromQuery(string query)
{
    string queryPath = query.Replace("query:.", args.ContextItemPath);
    return args.ContextDatabase.SelectItem(queryPath);
}

This will then return the matching item that is the parent of the datasources.

Upvotes: 2

Related Questions