2

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?

1
  • Both answers bellow are good.. Depends on whether the current context means anything or not in your situation
    – Zeb
    May 29, 2012 at 22:38

2 Answers 2

5

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.

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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