i want to get host header from web application programatically: for example in below code i got absolute url of web application "http://example.com" in RootUrl variable, from this web application, i want to get host header "example.com", how it can be done?

RootUrl = SPContext.Current.Site.WebApplication.GetAbsoluteUrl();

2 Answers 2


Please see the below link https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.hostname(v=office.12).aspx

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


The following code should provide the details you need, depending on if you want the root site, or site collection URL, or host name.

// Root Site – eg. Web App URL
var rootSiteCollectionURL = SPContext.Current.Web.Site.WebApplication.Sites[0].Url;

// Site Collection URL
var SiteCollectionURL = SPContext.Current.Web.Site.Url;

// Host Name of current Site Collection (Machine Name)
var HostName - SPContext.Current.Site.HostName

Your Answer

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

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