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
    use SP.Site.get_rootWeb() in some variable & compare it with your existing – Hardik Apr 25 '16 at 13:16
2

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;
  • Question is about root site collection, not root site. – dstarkowski Apr 25 '16 at 14:07
  • @dstarkowski you are right, don't know why I didn't see that :) – Anders Aune Apr 25 '16 at 21:22

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.