Sharepoint How to identify your current site is rootsite or subsite (level of subsite)?

2.7k views Asked by At

I use "_spPageContextInfo.webAbsoluteUrl" to detect the site url.

How to identity your current site which you are surfing is root site or subsite by javascript? If subsite, which level of subsite is it? The solution is only javascript and only bases on the site url.

Thanks for helping.

1

There are 1 answers

2
Gautam Sheth On

You can use _spPageContextInfo.siteServerRelativeUrl.

If _spPageContextInfo.siteServerRelativeUrl equals to / then you are definitely in the root site.

In the subsite, the value of _spPageContextInfo.siteServerRelativeUrl would be /subsite1.

In the sub-subsite, the value of _spPageContextInfo.siteServerRelativeUrl would be /subsite1/subsite1_1.

Another option would be, to check if _spPageContextInfo.webAbsoluteUrl equals to _spPageContextInfo.siteAbsoluteUrl. This will be true only in case of the root site. So, if(_spPageContextInfo.webAbsoluteUrl == _spPageContextInfo.siteAbsoluteUrl), then you would be in the root site.

To detect the level of subsite, you can try it as below:

var value = _spPageContextInfo.webServerRelativeUrl;

var level = value.split('/').length -1;