How to get a domain name for a Sitecore element

I have an installation on several Sitecore sites. I am trying to create an email that is sent that contains links to the Sitecore editor for each item. Everything works for me, except for the correct domain name. Given an item id, how can I use the API to get the domain for this item? For example, I want to create a URL like this:

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 get the "www.mySite1.com" part. Any ideas? I can never find such things in the Sitecore API, although I'm sure it is somewhere there.

+4
source share
3 answers

I assume that you have several sites configured in the <sites> section of your web.config file, and that each of them has a hostName property defined for example.

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

You can use the SiteManager class to access information about each site, including the name of the site.

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

.. If you are working on a Sitecore page, you can access the currently running Site object using

 Sitecore.Context.Site 

Hope this helps :)

+5
source

I think there are several approaches for you:

  • In the <linkManager> configuration, set alwaysIncludeServerUrl to true , so the calls to GetItemUrl() have absolute full URLs.

  • Set the barely documented targetHostName attribute to <site ... /> node in web.config and make sure the Sitecore Rendering.SiteResolving configuration parameter is set to true . This should then resolve the correct host name provided on the site in context.

+3
source

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

0
source

Source: https://habr.com/ru/post/1380232/


All Articles