Had an issue with Startpath for page editor and preview mode in Multi-site environment. It is always pointing to “sitecore/content/home” but I have different collection of sites that using branch templates like Siteocore/content/sitecollection/sitename1/home

It is working fine in Web. Here it is a solution that helped me. Calling the GetContentSite() method and setting the startpath in session and used it throughout the application.

 

public static Sitecore.Sites.SiteContext GetContextSite()
{
    if (Sitecore.Context.PageMode.IsPageEditor || Sitecore.Context.PageMode.IsPreview)
    {
        // item ID for page editor and front-end preview mode
        string id = Sitecore.Web.WebUtil.GetQueryString("sc_itemid");
 
        // by default, get the item assuming Presentation Preview tool (embedded preview in shell)
        var item = Sitecore.Context.Item;
 
        // if a query string ID was found, get the item for page editor and front-end preview mode
        if (!string.IsNullOrEmpty(id))
        {
            item = Sitecore.Context.Database.GetItem(id);
        }
 
        // loop through all configured sites
        foreach (var site in Sitecore.Configuration.Factory.GetSiteInfoList())
        {
            // get this site's home page item
            var homePage = Sitecore.Context.Database.GetItem(site.RootPath + site.StartItem);
 
            // if the item lives within this site, this is our context site
            if (homePage != null && homePage.Axes.IsAncestorOf(item))
            {
                return Sitecore.Configuration.Factory.GetSite(site.Name);
            }
        }
 
        // fallback and assume context site
        return Sitecore.Context.Site;
    }
    else
    {
        // standard context site resolution via hostname, virtual/physical path, and port number
        return Sitecore.Context.Site;
    }
}

Thanks Mark (Mark Ursino) for the nice work-around.

Update: Sitecore has also provided the patch (https://kb.sitecore.net/articles/382913) but it was giving warning message in experience editor so I used above code to fix the issue.


1 Comment

Sitecore web application development : Best Practices | Programming Hero · January 30, 2017 at 8:33 pm

[…] Good read: Sitecore Context Site: Start path is not available in “Master” DB for Multi-site […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Fake DB

How to do unit testing for media items in Sitecore + xUnit testing : Fake DB

Unit testing in Sitecore is a tricky one since you need create an items in Fake dB. I didn’t find much information about Media items unit testing and below piece of code may help you! Read more…

Fake DB

PageContext item in Sitecore Fake DB + Unit test cases

I tried to set PageContext using  RenderingContext.EnterContext but didn’t work using (RenderingContext.EnterContext(new Rendering(), contextItem)) { I have set it using  ContextService.Get().Push(pageContext); Here is the code, which may help you using (Db db = new Db using (Db Read more…

Sitecore 8.2

Mocking a reference field in Sitecore Fake DB for Unit test cases

I’m working on Unit test cases and mocking a reference field could be a complex task. Here is the code, which may help you [Fact] public void ReferenceFieldMockingTest() { ID newTemplateId = ID.NewID; ID referenceItemId Read more…