November Happy Hour will be moved to Thursday December 5th.

Manoj Kumawat
Aug 26, 2019
  3611
(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
AsyncHelper can be considered harmful

.NET developers have been in the transition to move from synchronous APIs to asynchronous API. That was boosted a lot by await/async keyword of C#...

Quan Mai | Dec 4, 2024 | Syndicated blog

The search for dictionary key

Recently I helped to chase down a ghost (and you might be surprised to know that I, for most part, spend hours to be a ghostbuster, it could be fun...

Quan Mai | Dec 4, 2024 | Syndicated blog

Shared optimizely cart between non-optimizley front end site

E-commerce ecosystems often demand a seamless shopping experience where users can shop across multiple sites using a single cart. Sharing a cart...

PuneetGarg | Dec 3, 2024

CMS Core 12.22.0 delisted from Nuget feed

We have decided to delist version 12.22.0 of the CMS Core packages from our Nuget feed, following the discovery of a bug that affects rendering of...

Magnus Rahl | Dec 3, 2024

Force Login to Optimizely DXP Environments using an Authorization Filter

When working with sites deployed to the Optimizely DXP, you may want to restrict access to the site in a particular environment to only authenticat...

Chris Sharp | Dec 2, 2024 | Syndicated blog

Video Guides: Image Generation Features in Optimizely

The AI Assistant for Optimizely now integrates seamlessly with Recraft AI, providing advanced image generation capabilities directly within your...

Luc Gosso (MVP) | Dec 1, 2024 | Syndicated blog