allBlogsList

Template Source in a Sitecore Multisite Solution

In general when creating a template and using fields like drop tree and tree list you want to limit the scope of the site to just the content tree for that site.  In the Source field you would add something like /sitecore/content/mysite/ .  The author would then have access to everything under the mysite tree.  But what if you have a template that is shared across several sites? Obviously in a scenario where a template is shared among several sites, you can't limit the scope to just one specific site, so you have to get a bit more creative.  In my specific scenario I was creating a global navigation module,  I had a field called "Internal Links" and wanted to limit this to the context of the current site that the data item lives under. Or another way to look at it.  I would have a navigation item located at /sitecore/content/mysite/data/navigation/navitem and when the item exists here I would want to be able to find my "mysite" folder and use that as the root of my linking. Here are the steps to accomplish this.

  1. Create a new template and call it "Site Folder".  Give it a proper icon, permissions, and any common insert options.

  2. Keep track of the template guid.  In my case and this example my guid is: {77AD33D0-BFB5-48A7-A365-678C6BF7FC70}

  3. Replace all your site folders with your new "Site Folder" template.

  4. Finally on the source section of the field you want to dynamically reference the link add this query:

    query:./ancestor::*[@@templateid='{77AD33D0-BFB5-48A7-A365-678C6BF7FC70}']
    

What we did here was from the context of my item search for any ancestors that have the template id we provided.  Since all of our sites now use the "Site Folder" template we should always be linking from the root of the site.  However if query fails, you will just see the full accessible Sitecore tree. From here we could attach /home to gain access to only stuff under "home" for my site.  We could also use a different template to limit access to a specific area under data or an area under the media library.  Overall the options for query are fairly flexible. For reference on query structure. And finally I highly recommend using the Xpath builder found in the Sitecore Admin for testing your queries.  This is found under the Developer Center and under the Tool menu.  This allows you to set the context of the item you want to call the query from, the query itself, and a list of results assuming the query works.