2

I have an Sitecore (v8.1) Event and method that processing this event but Sitecore.Context is not available.

I am try to use call SiteContextSwitcher but it does't help.

SiteContext siteContext = SiteContext.GetSite(site);
                using (new SiteContextSwitcher(siteContext))
                {
                    Sitecore.Context.SetActiveSite(site);
                    ....
}

What is the best option to get context in event ?

6
  • What part of the context do you need? What I can see is that you are trying to set the context site, but that will not do if you need something else. Can you give us some information on what exactly you are trying to do?
    – Gatogordo
    Jan 17, 2017 at 14:31
  • @Gatogordo by legacy reason I need to have context behavior similar to when user open default(first/main) page of site. Jan 17, 2017 at 14:33
  • But what do you need from the context? The site, the database, the item, ... ?
    – Gatogordo
    Jan 17, 2017 at 14:38
  • @Gatogordo I need everything Jan 17, 2017 at 14:39
  • Why? Just asking because it seems like your code is coupled to that Context too much - you might be able to get the same result otherwise. So before trying SiteContextSwitcher/DatabaseSwitcher/LanguageSwitcher/... and so so: what are actually trying to do?
    – Gatogordo
    Jan 17, 2017 at 14:45

2 Answers 2

2

You can do it in the next way:

  1. First take the current item when the event was triggered

    Item item = Event.ExtractParameter(args, 0) as Item;
    
  2. Get the current site of the item :

    Sitecore.Web.SiteInfo siteInfo = Sitecore.Configuration.Factory.GetSiteInfoList()
    .FirstOrDefault(x => item.Paths.FullPath.StartsWith(x.RootPath));
    
    var siteContext=Sitecore.Configuration.Factory.GetSite(siteInfo.Name)
    using (new SiteContextSwitcher(siteContext))
            {
               //your logic 
            }
    
5
  • 1
    you beat me ;) about to paste similar code Jan 17, 2017 at 14:34
  • @sitecoreclimber thanks, but how to set context in this case ? Jan 17, 2017 at 14:37
  • 1
    This will only get the site, not set the whole context as OP needs apparently.
    – Gatogordo
    Jan 17, 2017 at 14:40
  • I updated my answer Jan 17, 2017 at 14:45
  • Thanks for answer, I have recognized that for just switch context is not enough... set site language should be done as well from code... Jan 18, 2017 at 9:23
2

similar to @sitecoreClimbers answer. I have written an extension method for item to do this.

Get Item from Event arguments:

var item = (Item)Event.ExtractParameter(args, 0);

Extension method of item to get siteInfo:

public static SiteInfo GetSiteInfo(this Item item)
    {
        var siteInfoList = Factory.GetSiteInfoList();

        SiteInfo currentSiteinfo = null;
        var matchLength = 0;
        foreach (var siteInfo in siteInfoList)
        {
            if (!item.Paths.FullPath.StartsWith(siteInfo.RootPath, StringComparison.OrdinalIgnoreCase) || siteInfo.RootPath.Length <= matchLength)
                continue;
            matchLength = siteInfo.RootPath.Length;
            currentSiteinfo = siteInfo;
        }
        return currentSiteinfo;
    }

Setting site context:

    var context = Sitecore.Configuration.Factory.GetSite(item.GetSiteInfo().Name);
    context.SetActiveSite(item.GetSiteInfo().Name);
    using (new SiteContextSwitcher(context.Site))
    {
        //Logic
    }
2
  • 1
    Same comment: this will only get the site, not set the whole context as OP needs apparently
    – Gatogordo
    Jan 17, 2017 at 14:41
  • agree, updated! Jan 17, 2017 at 15:02

Your Answer

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

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