Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top