Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

For example my site collections structure is as follows:

http://testserver1.com/sites/sitecollection1

http://testserver1.com/sites/sitecollection2

http://testserver1.com/sitecollection3

http://testserver1.com/team/sitecollection4

I want to get http://testserver1.com (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
add comment

2 Answers 2

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
add comment

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
add comment

Your Answer

 
discard

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.