How to get the domain name for a Sitecore item
Asked Answered
D

3

5

I have a multi site Sitecore installation. I am trying to create an email that gets sent out that contains links back to the Sitecore editor for each item. I have it all working except the correct domain name. Given an item ID how would I use the API to get the domain for that item? For example - I want to produce a URL like the following:

http://www.mySite1.com/sitecore/shell/sitecore/content/Applications/Content Editor.aspx?id=id

The only part I'm not sure about is how to programmatically retrieve the "www.mySite1.com" part. Any ideas? I can never find stuff like this in the Sitecore API although I am sure it is there somewhere.

Dispart answered 8/11, 2011 at 16:33 Comment(1)
I'm not sure that that functionality is in there... at least not in the earlier version of Sitecore 6. Sitecore is more or less built to be a single domain solution, though you can overcome most of the problems with it. But this problem usually requires some custom code to give you the correct domain.Maulmain
F
7

I am assuming you have multiple sites set up in the <sites> section of your web.config file and that each one has a hostName property defined e.g.

<site name="website1" hostName="website1.com" ...
<site name="website2" hostName="website2.com" ...

You can use the SiteManager class to access info about each sites including the hostName

Sitecore.Sites.SiteManager.GetSite("website1").Properties["hostName"]

.. if you are running in a Sitecore page you can access the currently running Site object using

Sitecore.Context.Site 

Hope this helps :)

Fantasist answered 10/11, 2011 at 10:53 Comment(0)
S
4

I think there are a few approaches for you:

  1. In your <linkManager> config, set alwaysIncludeServerUrl to true so calls to GetItemUrl() have absolute full URLs.

  2. Set the barely documented targetHostName attribute on the <site ... /> node in your web.config and ensure the Sitecore config setting Rendering.SiteResolving is set to true. This should then resolve the correct target hostname given the site in context.

Sparkman answered 3/4, 2012 at 1:41 Comment(0)
N
2

I'm not sure this will work in a multi site environment but you could call Sitecore.Globals.ServerUrl. If this wont work you can get the hostname by calling Sitecore.Context.Site.HostName, Sitecore.Context.Site.TargetHostName or Sitecore.Context.Site.Properties["hostName"] if you access it via a page as previously mentioned.

Nummulite answered 23/9, 2015 at 9:20 Comment(1)
I have multi domains that one for sitecore, one for other third party app. They share the same sitecore content for navigation banner. When navigate to the third party app, I need to use Sitecore.Globals.ServerUrl to get back to 'HOME' url which is the main sitecore domain.Infertile

© 2022 - 2024 — McMap. All rights reserved.