SaaS CMS has officially launched! Learn more now.

Manoj Kumawat
Aug 26, 2019
  3356
(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
Creating Custom Actors for Optimizely Forms

Optimizely Forms is a powerful tool for creating web forms for various purposes such as registrations, job applications, surveys, etc. By default,...

Nahid | Jul 16, 2024

Optimizely SaaS CMS Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024

How to have a link plugin with extra link id attribute in TinyMce

Introduce Optimizely CMS Editing is using TinyMce for editing rich-text content. We need to use this control a lot in CMS site for kind of WYSWYG...

Binh Nguyen Thi | Jul 13, 2024

Create your first demo site with Optimizely SaaS/Visual Builder

Hello everyone, We are very excited about the launch of our SaaS CMS and the new Visual Builder that comes with it. Since it is the first time you'...

Patrick Lam | Jul 11, 2024