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


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. 


Jun 25, 2021 7:13

Give a try to this-

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


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";
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.

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.

Jun 25, 2021 9:39
* 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.