4

I often use the inherits attribute of the <site> configuration node to make one site inherit another. Today, another SSE question made me ask myself how I can check to see if the context site inherits a specific site. I tried to find something in the Sitecore API for this but came up empty.

Does anyone know if there is a way to check if a site inherits another, aside from directly grabbing the Node from configuration and checking the attribute manually?

  • Would this be a userful SPE command? – Michael West Nov 23 '16 at 22:36
  • Yes! Yes, it would @MichaelWest! If only we knew someone on the SPE dev team who could add a command for it... ;) – Zachary Kniebel Nov 23 '16 at 22:39
  • 1
    Great. I'll see if I can offshore the work. – Michael West Nov 24 '16 at 0:32
4

Here is a low complexity way of checking using Sitecore PowerShell Extensions. The solution is not very polished, but gets the job done.


First I wrote a script to check inheritance and saved it to a script library.

X-Demo/Content Editor/Context Menu/Check Inheritance

Close-Window
function Get-SiteConfiguration {
    param(
        [string]$Name
    )

    [Sitecore.Sites.SiteManager]::GetSite($Name).Properties
}

function Get-SiteName {
    param(
        [item]$Item
    )

    $itemName = $Item.Name
    [Sitecore.Configuration.Factory]::GetSiteInfoList() | 
        Where-Object { $_.RootPath -eq "/sitecore/content" -and $_.StartItem -eq "/$($itemName)" }
}

$sites = Get-SiteName -Item (Get-Item -Path .)
$inheritedName = $null
$siteName = $null
foreach($site in $sites) {
    $siteName = $site.Name
    $siteConfig = Get-SiteConfiguration -Name $site.Name
    foreach($setting in $siteConfig) {
        if($setting.Key -eq "inherits") {
            $inheritedName = $setting.Value
            break
        }
    }
}

if($inheritedName) {
    Show-Alert "The item has a parent site of '$($siteName)' which inherits from '$($inheritedName)'."
} else {
    Show-Alert "The item has a parent site of '$($siteName)'."    
}

Second I added a simple rule so the check only works when you select the home item.

Script rule

Now when I see this context menu script and alert message.

Context Menu

Checking /homeinherited

Inherited site

Checking /home

Non-inherited site

Perhaps not the most elegant solution, but for zero downtime and in the time it took to eat a bowl of cereal it gets the job done; looking for an icon may have taken all night though. This solution is essentially what Dmytro suggested but without the need to deploy compiled code on the server.

3

You can check the site properties:

var site = Sitecore.Context.Site;
// or site = Sitecore.Configuration.Factory.GetSite("mywebsite");

string inherits = site.Properties["inherits"];

If the site has been configured with the inherits property then the return value will be the name of the website set, otherwise the value will be null.

2

There is no API for this. Inheritance information is not preserved after parsing configuration files.

In the Sitecore.Sites.ConfigSiteProvider, here's the method that deals with site inheritance:

private void ResolveInheritance(SiteCollection sites, SafeDictionary<string, Site> siteDictionary)
{
  foreach (Site site in (Collection<Site>) sites)
  {
    if (!string.IsNullOrEmpty(site.Properties["inherits"]))
    {
      this.AddInheritedProperties(site, siteDictionary);
    }
  }
}

And here's the decompiled code of AddInheritedProperties:

private void AddInheritedProperties(Site site, SafeDictionary<string, Site> siteDictionary)
{
  string index = site.Properties["inherits"];
  Site site1 = siteDictionary[index];
  Assert.IsNotNull((object) site1, "Could not find base site '{0}' for site '{1}'.", new object[2] { index, site.Name });

  foreach (KeyValuePair<string, string> property in (SafeDictionary<string, string>) site1.Properties)
  {
    if (!site.Properties.ContainsKey(property.Key))
    {
      site.Properties[property.Key] = property.Value;
    }
  }
}

As you can see, it just copies all properties from the original site definition to the inherited one. Inheritance metadata is not preserved.

As a conclusion, if you need to know site inheritance in your code, you'll have to parse Sitecore configuration manually.

  • You beat me by seconds :) – Søren Kruse Nov 23 '16 at 20:59
  • Thanks Dmytro. That's what I came up with, as well. Good to know I'm not missing something silly. – Zachary Kniebel Nov 23 '16 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.