SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)



share|improve this question

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;
share|improve this answer

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.

share|improve this answer
Thanks, but I need the root web of the ROOT SITECOLLECTION – Ruben Herman Jul 3 '12 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. – Falak Mahmood Jul 3 '12 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. – Falak Mahmood Jul 3 '12 at 11:09

You can use the _spPageContextInfo global object in SharePoint JavaScript:

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

share|improve this answer
up vote 0 down vote accepted

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...

share|improve this answer

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.

share|improve this answer

Your Answer


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.