May 26, 2011

Add a custom property to the site node

Here is an easy way to add and retrieve a custom property to you site in the .config file.


Here is an example with my custom property SiteUID
  1. Add this property to your site tag:
    <site patch:after="*[contains(@name, 'login')]"
        hostName="uk.xxx"
        targetHostName="uk.xxx"
        name="UK"
        siteUID="MyUniqueID"
        virtualFolder="/"
        physicalFolder="/"
        rootPath="/sitecore/content/United Kingdom/Website"
        startItem="/Homepage"
        contentStartItem="/sitecore/content/United Kingdom/Website/Homepage"
        database="web"
        domain="extranet"
        allowDebug="true"
        cacheHtml="true"
        htmlCacheSize="10MB"
        registryCacheSize="0"
        viewStateCacheSize="0"
        xslCacheSize="5MB"
        filteredItemsCacheSize="2MB"
        enablePreview="true"
        enableWebEdit="true"
        enableDebugger="true"
        disableClientData="false"
        language="en"/>
  2. Create an extension method:
    /// <summary>
    /// Retrun the site unique ID
    /// </summary>
    /// <returns></returns>
    public static string SiteUID(this SiteContext site)
    {
        Assert.IsNotNull(site, "Site cannot be null");
                
        try 
        {         
            string uid = site.Properties["siteUID"];
            if (!String.IsNullOrEmpty(uid))
                return uid;
            else
                return site.Name;
        }
        catch (Exception)
        {
            return site.Name;
        }
    }
  3. Use this property in your code: (don't forget to add you using to the class with your extention method)
    Sitecore.Context.Site.SiteUID()

No comments:

Post a Comment