Try our conversational search powered by Generative AI!

Get the current site ID or site settings from the schedule job

Vote:
 

Hi 
how can we get the current site ID or site settings from the schedule job. I have tried with HttpContext.Current but it's giving null because jobs are not executed on a web request. I have to update the page/block by using the schedule job. 

Thanks

#257106
Jun 25, 2021 7:13
Vote:
 

Give a try to this-

 var currentSiteDef = SiteDefinition.Current;
#257109
Jun 25, 2021 9:00
Vote:
 

Hi,

Scheduled jobs don't really execute in the context of a specific site so I suspect SiteDefinition.Current might not work. I tend to use ISiteDefinitionRepository to get all site instances and just pick one (either a specific one or, if the specific site's not important, the first) like this:

var currentSite = SiteDefinition.Current;
if (currentSite == null || ContentReference.IsNullOrEmpty(currentSite.StartPage))
{
    currentSite = ServiceLocator.Current.GetInstance<ISiteDefinitionRepository>().List().FirstOrDefault();
}
if (currentSite == null)
{
    return "No sites defined";
}
#257110
Edited, Jun 25, 2021 9:21
Ravindra S. Rathore - Jun 25, 2021 9:35
Hi Paul, I think "SiteDefinition.Current" should work. I used it in one project and if I remember correctly we retrieved the site base URL from this
Paul Gruffydd - Jun 25, 2021 9:44
Yeah, it'll work in most scenarios as long as there's a wildcard host mapping (or the current host maps to a site, though I'm not sure that would work without an HTTP context). It's always worthwhile having a fallback just in case though.
Vote:
 

Actually, looking at the comment on SiteDefinition.Current, you can ensure a site definition is always returned by setting a wildcard host mapping (*) in the site configuration in admin mode though it's still worthwhile handling the situation where a site definition can't be resolved.

#257112
Jun 25, 2021 9:39
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.