torsdag den 18. september 2014

Sublayout caching disabled when sublayout is hidden by serverside code

Issue

I have a leftmenu that can be configured on the page-template to be hidden using a Sitecore field. When the editor sets a checkmark in the HideLeftMenu field the placeholder containing the sc:Sublayout control is hidden. This seems to result in that caching of the Sublayout is ignored.
The code looks like this

Markup

 <asp:PlaceHolder runat="server" ID="phLeftColumn">  
   <div class="col-sm-4 col-md-3">  
     <asp:ContentPlaceHolder runat="server" ID="cphPageContentLeft">  
       <dom:Sublayout runat="server" ID="submenu" Path="/layouts/LeftMenu.ascx" Cacheable="True" VaryByData="True" />  
     </asp:ContentPlaceHolder>  
   </div>  
 </asp:PlaceHolder>  

Code behind

 phLeftColumn.Visible = Sitecore.Context.Item["HideLeftMenu"] != "1";   

How to reproduce

You have following code in your layout
 <asp:PlaceHolder runat="server" Visible="true">  
   <sc:Sublayout runat="server" Path="Sublayout.ascx" Cacheable="true" />  
 </asp:PlaceHolder>  

And you have following code in your Sublayout.ascx
 protected void Page_Load(object sender, EventArgs e)  
 {  
     Response.Write("Sitecore Cache is not in play");  
 }  

When refreshing the page you will see that "Sitecore Cache is not in play" is only shown the first time due to Sitecore caching which serves the persisted html after the first load resulting in Page_Load not being called.

I now change Visible attribute of the asp:Placeholder to false.
When refreshing the page you will see that "Sitecore Cache is not in play" will be displayed everytime.
This points in the direction that Sitecore caching is for some reason disabled when the Sublayout is hidden.

 

Solutions

lørdag den 23. august 2014

Automatic Multisite Handling By Traversing Sitecore Tree

Add automatic multisite handling to your Sitecore site by traversing the Sitecore Tree to find the available sites. You can decide to use the site name as subdomain or you can specify a hostName in a Sitecore field on the site-item.


 

 

 

 

Web.config changes

Adding a config section for configuring the multisite handler.

 <configSections>  
      <section name="Netmester.ItemResolver" type="Netmester.ItemResolver.Configuration, Netmester.ItemResolver"/>  
 </configSections>  
 <Netmester.ItemResolver SitecoreWebSiteName="subsites" UrlSyntax="{$}.mydomain.dk|www.{$}.mydomain.dk" HostNameField="hostNames" TargetHostName="targetHostName" />  

  • SitecoreWebSiteName -  The name of the site defined in tag in web.config. Is used as template when the sites are created.
  • UrlSyntax - Specifies the syntax used as hostName for each site created. 
  • HostNameField - Specifies a field on the site-element that should be used as hostName. Pipes can be used.
  • TargetHostName - Specifies a field on the site-element that should be used as targetHostName. If none is specified the first one defined as hostName is used.

Override the standard sitecore siteprovider.

 <siteManager defaultProvider="config">  
      <providers>  
           <clear/>  
           <add name="config" type="Netmester.ItemResolver.SiteProvider, Netmester.ItemResolver" siteList="sites" checkSecurity="false"/>  
      </providers>  
 </siteManager>  

Code

Configuration class

Class associated with the config section defined in web.config

Faste læsere