Try our conversational search powered by Generative AI!

Getting the site Id in a class library


I have a question about getting the site id from a class library in episerver. I figure it's simple, but with the keywords episerver, site and id I came up with a sea of irrelevant results.

I am using episerver 9. The only way I have found so far involves using the SiteDefinitionResolver class and the current page. I need access to the site id in a class library. Is there some other way to get the current site id? I guess I could pass it in from the UI, but that seems a bit ugly as we have put wrappers around the episerver wrappers to remove as many dependencies to episerver from our UI and service layer as possible.


Feb 01, 2016 20:37

You are correct. 

var resolver = ServiceLocator.Current.GetInstance<SiteDefinitionResolver>();
var site = resolver.GetDefinitionForContent(page.ContentLink);
var siteId = site.Id;
Earlier versions of EPiServer had a site tag in the config files you could grab. Now the site id is stored in the database so using the SiteDefinitionResolver is the way to go
Edited, Feb 01, 2016 22:49

Hello Frank

It depends on your exact requirements but you should be able to use the ServiceLocator (or constructor injection) to get an instance of SiteDefinitionRepository then use the Get method with the current HttpContext to get the current site defintion which includes the Id. If you want to write tests around this you can mock the SiteDefinitionRepository implementation.


Edited, Feb 01, 2016 22:59

Thanks David, that was what I was looking for.

Feb 01, 2016 23:05
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.