Try our conversational search powered by Generative AI!

Manoj Kumawat
Aug 26, 2019
  3238
(4 votes)

Accessing ContentReference in schedule job or Initialization module where you receive HttpContext as null

Often you want to work with content in an initialization module or a scheduled job and use a reference to the Start page. But in these scenarios, there is no HTTP context, so the system does not know what site you want to work with. 

One fix is to set a wildcard * as a hostname in the configuration of one of you sites. This site will then be returned by ContentReference.StartPage.

Here is an example of how you can customize this. Go to your website > Admin> config > Manage Websites and do the following - 

It should now give you a reference of StartPage of the site, see below- 

Better Alternative

A more flexible alternative is to use the ISiteDefinitionRepository to get a list of all sites and write logic to select the one you want to work with.

Here is how you can make usage of this repository - 

var siteDefinitionRepo = ServiceLocator.Current.GetInstance<ISiteDefinitionRepository>();
var siteDefinitions = siteDefinitionRepo.List().ToList();

Now you can work with the site item which we've just got via this Repository.

On a side note, However I've created an instance of above repository just for a demo purpose but you should always inject your dependencies and get them resolved with IOC container like StructureMap which Episerver recommends.

Have a great day!

Aug 26, 2019

Comments

valdis
valdis Aug 26, 2019 07:06 PM

I was about to write about ServiceLocator and then read your last sentence ;))

Please login to comment.
Latest blogs
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog