3

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 Apr 14 '16 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 Apr 14 '16 at 13:07
  • @Detilium, Is my answer works for you? – Hardik May 2 '16 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 May 3 '16 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 May 3 '16 at 11:23
8

CSOM

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

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

JSOM

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 Apr 14 '16 at 12:51
  • My bad, too much into JSOM ! – JayHell Apr 14 '16 at 12:53
1

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.load(siteCollection);
    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 Sep 20 '18 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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