SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For example my site collections structure is as follows:

I want to get (host header) from the code? how can I do it using object model? Is there a problem if we have Alternate access mapping being configured on these site collections as well?

share|improve this question
Both answers bellow are good.. Depends on whether the current context means anything or not in your situation – Zeb May 29 '12 at 22:38
up vote 5 down vote accepted

You can use SPContext.Current.Site.HostName property to get the host name?

Normally you can use SPContext.Current.Site.Url to return the Url of the root website in the site collection for the current web request. If you want the context of the current web site then use SPContext.Current.Web.Url.

share|improve this answer

When you say "host header," I assume you are referring to the web application to which your site collections belong. With that in mind, you can use the SPWebApplication's AlternateUrls collection to get the host header(s) associated with that web application.

Since it is possible to have multiple URLs associated with a single web application, you would need to iterate through this collection to get/find the host header you want. For instance:

SPAlternateUrlCollection allUrls = SPContext.Current.Site.WebApplication.AlternateUrls;
foreach (SPAlternateUrl url in allUrls)
    // Do something with each url.Uri.ToString()

Note that you will need to add a reference to Microsoft.SharePoint.Administration to make use of this code.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.