2

I have a situation in which I'd like to know whether the user is currently on their root site collection or not.

I tried the following:

var siteRelativeUrl = ctx.Web.ServerRelativeUrl;
var isRoot = siteRelativeUrl.Equals("/");

But apparently this fails some times, so I guess you can have a none root site collection consisting of https://test.sharepoint.com/.

I was wondering and hoping that CSOM had some way of checking this, or if any had some suggestions as of what to do?

1
  • 1
    use SP.Site.get_rootWeb() in some variable & compare it with your existing
    – Hardik
    Commented Apr 25, 2016 at 13:16

1 Answer 1

3

You could compare the ID with the RootWeb:

clientContext.Load(clientContext.Site.RootWeb, rw => rw.Id);
clientContext.Load(clientContext.Web, w => w.Id);
clientContext.ExecuteQuery();

var isRoot = (clientContext.Site.RootWeb.Id == clientContext.Web.Id) ? true : false;
2
  • Question is about root site collection, not root site. Commented Apr 25, 2016 at 14:07
  • @dstarkowski you are right, don't know why I didn't see that :) Commented Apr 25, 2016 at 21:22

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.