2
votes

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.

1
I'm not sure I fully understood. Datasource queries are supported in Sitecore 8 MVC, this has nothing to do with Glass, which itself supports queries. As long as your query is resolved to a correct item then the item should be passed to Glass. Can you provide same of the code/branch structure that you are using?jammykam
Maybe I'm just using it incorrectly. Do you know how to give a view rendering's datasource a query that gets an item that is a child of the current item with the name Test Content?Teeknow

1 Answers

2
votes

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.