0

I am trying to get to my site settings item in my tree to see what office id I need between my US site and my CA version. I have tried Sitecore.Context.Site but since this is a scheduled task it comes through null, so just wondering if there is a work around to try and get access in this class to my tree again.

enter image description here

3
  • Could you please provide more details? Do you have a single scheduled task that does the same thing for 2 different sites? Can you break it up into 2 scheduled tasks so that it's clear for which site it's running? You can always do Sitecore.Context.Database.GetItem(relevantSiteSettingsItemId). If you don't want to use the Item Id, you could also use the item path. – Sasha Kachanov Apr 2 at 17:39
  • This task is for processing our forms that we submitted. We then are doing a post to an external api that we need to send the office ID based on what is in the sitesettings for that site. The item ID wouldnt work since I need it dynamic, to tell me if sitesetting is for CA it gives me office id of 2 and if I am on US then it would give me 1. I think getting it by path might work since the id is different per site. – user1566783 Apr 2 at 17:49
  • If you are using path then it should be configurable via item so that if path change in near future then you just update the item otherwise item id also as a configurable item – Amit Kumar Apr 2 at 20:09
0

Depending on your scheduler implementation you can use SiteContextSwitcher to switch Sitecore.Context.Site to your actual site context.

First you need to get the website either by name:

var website = Factory.GetSite("yoursitename");

or if you have an item object you can get its site by the path

var siteInfo = Sitecore.Configuration.Factory.GetSiteInfoList().FirstOrDefault(x => item.Paths.FullPath.StartsWith(x.RootPath));    
var website = Sitecore.Configuration.Factory.GetSite(siteInfo.Name)

using (new SiteContextSwitcher(website))
{
   //implement your logic here
}

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.