How is it possible to check in JS/jQuery if the user is on the root site of the root sitecollection in the webapplication? (Sharepoint 2010)




5 Answers 5


I like @Vardhaman Deshpande's answer. But as I understand the question, we must check if it is on the rootweb (root site) in a site collection. A site collection can have different url than "/". But using _spPageContextInfo is absolutely right. So in javascript, to determine if it is a root web or not:

var siteUrl = _spPageContextInfo.siteServerRelativeUrl;
var webUrl = _spPageContextInfo.webServerRelativeUrl;
var isRootWeb = siteUrl == webUrl;

You can use ECMAScript to achieve it.

var clientContext = new SP.ClientContext();
var rootWeb = clientContext.get_site().get_rootWeb(); //always return the root web of the site collection

See some examples here.

  • Thanks, but I need the root web of the ROOT SITECOLLECTION Commented Jul 3, 2012 at 10:11
  • The scope of client object model is site collection, you cannot access to the content out of the current site collection. You'll need to manually use the web services on the client side, perhaps using SPServices, to interact with the root site collection. Commented Jul 3, 2012 at 10:56
  • An alternative solution to your problem could be to put a property bag in the root web of your root site collection and try to find it using ECMAScript. If you find the property bag then you are on root web of root site collection else it's other site collection. Commented Jul 3, 2012 at 11:09

You can use the _spPageContextInfo global object in SharePoint JavaScript: http://blog.tedpattison.net/Lists/Posts/Post.aspx?ID=9

The _spPageContextInfo.webServerRelativeUrl value will always be "/" if you are on the root site.


I've solved this by adding a usercontrol to the masterpage where I check the current url. If it passes as rootweb from sitecollection, then I use the scriptmanager to write a clientscriptblock into the page. I guess using a webservice was also a possibility, but the fastest way for me was this one...


If you had to do it client side, you could check the L_Menu_BaseUrl variable. If it's blank you're on a root site.

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.