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

Getting the site Id in a class library

Vote:
 

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.

Thanks

#143822
Feb 01, 2016 20:37
Vote:
 

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
#143827
Edited, Feb 01, 2016 22:49
Vote:
 

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.

http://world.episerver.com/documentation/Class-library/?documentId=cms/9/780BF6D8

David

#143828
Edited, Feb 01, 2016 22:59
Vote:
 

Thanks David, that was what I was looking for.

#143830
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.