I'm trying to evaluate the position of users, defining whether they come from a site (test.sharepoint.com/sites/site1) or if they reside at the root site collection (test.sharepoint.com).

Does CSOM have any way of achieving this?

If I'm at test.sharepoint.com/sites/site1, can I get the root site collection of that?

  • Just to clarify, you want to check if the user is at the root web application site collection (not nested under a managed path) ?
    – JayHell
    Commented Apr 14, 2016 at 13:04
  • Correct. I as hoping that the ClientContext would have information about this, but I can't seem to find any. Something context.Site.Parent or something similar.
    – Detilium
    Commented Apr 14, 2016 at 13:07
  • @Detilium, Is my answer works for you?
    – Hardik
    Commented May 2, 2016 at 12:19
  • @Hardik Your example shows how to do this with JSOM. I'm looking for a way to do this with CSOM.
    – Detilium
    Commented May 3, 2016 at 11:07
  • Please refer the links which i had provided in my answer. it will help you for both server side & client side code. I have also updated my answer as well so please look into it....... Thanks
    – Hardik
    Commented May 3, 2016 at 11:23

2 Answers 2



Using the ClientContext, get your Site object and check its URL

SPContext.Current.Site.ServerRelativeUrl : if == "/"


For that you can use the _spPageContextInfo variable.

To check if user is in root site collection vs any other site collection, check this variable : _spPageContextInfo.siteServerRelativeUrl : if == "/", then is the absolute root site collection

To check if user is in a subsite vs site collection, compare those variables :

_spPageContextInfo.webAbsoluteUrl : current absolute URL _spPageContextInfo.siteAbsoluteUrl : current site collection absolute URL

  • Sorry, I should have mentioned that I'm looking for a server-side way of achieving this.
    – Detilium
    Commented Apr 14, 2016 at 12:51
  • My bad, too much into JSOM !
    – JayHell
    Commented Apr 14, 2016 at 12:53

SPSite.RootWeb is the object you're after, in server side code ( http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.rootweb.aspx).

SP.Site.get_rootWeb() in client object model code ( http://msdn.microsoft.com/en-us/library/ee552834.aspx).

To get root site collection,

SPSite rootSiteCollection = mySiteCollection.WebApplication.Sites[0];

Below is the sample code to get ROOT Site Collection.

var clientContext;
var siteCollection;

// Make sure the SharePoint script file 'sp.js' is loaded before your
// code runs.
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);

// Create an instance of the current context and get the site collection.
function sharePointReady() {
    clientContext = SP.ClientContext.get_current();
    siteCollection = clientContext.get_site();

    clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);
function onRequestSucceeded() {
    alert('URL of the root website: ' + siteCollection.get_url());
function onRequestFailed(sender, args) {
    alert('Error: ' + args.get_message());
  • SPSite is not about CSOM, it is serverside developing.
    – Emaborsa
    Commented Sep 20, 2018 at 10:02

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.