Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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)

Thanks!

Example:

share|improve this question
add comment

5 Answers

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
add comment

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
add comment

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.

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

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
add comment

Your Answer

 
discard

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.