Try our conversational search powered by Generative AI!

Accessing CMS API outside of the request pipeline


We have built an admin page that allows site admins to select a page and press a button to copy that page out under a couple hundred container pages. The problem is, it takes a while so it times out. Currently, I'm trying to move this to execute on a background thread, but am running into the issue of ContentReference.Startpage being empty. Thanks to a post by Per Magne Skuseth in another thread, I found this quote from Ted Nyberg:

You may run into similar problems in scheduled jobs. One way around it is to add a wildcard binding (“*”) for your default site.

That will then be your default site definition (as in SiteDefinition.Current.StartPage or ContentReference.StartPage) when there’s no current site, or outside of web requests.

Is this referring to an IIS mapping or something within EPiServer? Right now, I'm running the site locally out of VS, but it will later be deployed to IIS7. How do I add this wildcard binding? He says "one way around it". Are there other ways? Are there other things in the API dependent on the http request that I won't be able to use to manage the site content from the background thread?

Jan 05, 2016 19:22

You can do this in admin mode under Config > Manage websites. You just add a new row to the host names with the value *. However, this works only if you only have one startpage/website in your Episerver instance.

Your other option is to have a setting in a config file where you can specify the startpage.

Jan 06, 2016 0:01

Which EPiServer version is this?

Jan 06, 2016 22:48

Version 8. Thanks Johan, that got it working for now. But in about 3 months we will probably add another site to the instance. I'm reading that the startPageId of the siteSettings config element is obsolete as of version 7. Is that the setting you're referring to?

If there are multiple sites, it makes sense for it not to just choose one to return the start page for without an HttpRequest, or my code specifying which site it wants a start page for. How can I go get the start page for the site I want programmatically?

Jan 08, 2016 15:42

If there are more than one website and you don't have an http context, you have to manully load the specific startpage you want to work with. When I was referring to a setting, I meant that you could add your own app setting for which startpage to use.

If you have the url (including the host) but no http context you can load the startpage via the SiteDefinitionResolver:

var siteDefinitionResolver = ServiceLocator.Current.GetInstance<SiteDefinitionResolver>();
var definition = siteDefinitionResolver.GetByHostName("", fallbackToWildcard: true);

var startPage = definition.StartPage;

But if your code is running under a http context ContentReference.StartPage should work as expected.

Hope this helps.

Jan 08, 2016 15:52
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.