1

In my code, I'm trying to access the root path for the current site.

Let's say I'm accessing my website http://mywebsite.

Here is the site definition:

<site
  patch:before="site[@name='website']"
  name="SomeSite"
  hostName="mywebsite"
  rootPath="/sitecore/content/Somesite"
  startItem="/home"
  database="master"
  loginPage="/login"
  ...
  />

When I try to get the root path, all methods I've tried return /sitecore/content. What I want to get is /sitecore/content/Somesite, which is what I've specified on the site definition.

I tried the following:

  • Sitecore.Context.Site.RootPath
  • foreach(var site in Sitecore.Configuration.Factory.GetSiteInfoList())
  • Sitecore.Context.Site.ContentStartPath (which returns null)

Is there something I'm missing, like passing the current hostname or something that can pick the complete root path?

2
  • Hello Santosh. Your original question was hard to understand, so I edited it. I think it became much more readable, but please feel free to edit it again if you believe I misinterpreted something. Commented Dec 10, 2016 at 15:22
  • I have removed all of your comments and pasted the article links to your answer instead. I did that because the comments were not relevant for your question, they were logically part of the answer. I have additionally reformatted the answer to make it more readable and added a somewhat detailed description. This way, other users will be able to better understand what your code samples do. Please try to follow this format in the future when asking questions and posting answers. Commented Dec 10, 2016 at 16:07

1 Answer 1

3

Here's how I solved the problem.

First, I created a helper method GetSiteContext that returns a SiteContext object based on a URL. This helper method iterates through all configured sites and tries to match them to the host name of the URL. If no site matches, the context of the website is returned.

public static SiteContext GetSiteContext(Uri requestUrl)
{
    Assert.ArgumentNotNull(requestUrl, "requestUrl");
    string requestHostName = requestUrl.Host;

    foreach (SiteInfo siteInfo in Factory.GetSiteInfoList())
    {
        if (IsMatch(requestHostName, siteInfo.HostName) || IsMatch(requestHostName, siteInfo.TargetHostName))
        {
            return new SiteContext(siteInfo);
        }
    }

    return SiteContext.GetSite("website");
}

private static bool IsMatch(string input, string wildcardPattern)
{
    if (string.IsNullOrWhiteSpace(input) || string.IsNullOrWhiteSpace(wildcardPattern))
    {
        return false;
    }

    string regexPattern = WildcardToRegex(wildcardPattern);
    return Regex.IsMatch(input, regexPattern, RegexOptions.IgnoreCase);
}

You can see a usage example below. In this code, I am obtaining a site context based on the current request URL.

var siteContext = GetSiteContext(HttpContext.Current.Request.Url);
Item item;

if (siteContext != null)
{
     item = DatabaseProvider.MasterDatabase.GetItem(siteContext.RootPath + siteContext.StartItem);
}

Articles that helped me in resolving the issue:

1
  • 1
    Thank you very much. I appreciate your help. This is really helpful. Commented Dec 12, 2016 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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