How to dynamically construct the SharePoint site relative url in JavaScript

There are times when you want to dynamically construct a SharePoint site relative Urls e.g. if you are calling a SharePoint webservice from JQuery etc. then you will need to call it in context of your required site.

Most people try to slice and dice the URL of the current page and try to find the SharePoint site from it. This is not a very clean way of doing it.

The more easy and clean way is the way SharePoint itself does which is using the JavaScript variable called L_Menu_BaseUrl

This variable holds the relative url of current site.

So for example, if you want to call Lists.asmx for

http://yourintranet/sites/Finance/ then

L_Menu_BaseUrl + ‘/_vti_bin/Lists.asmx’ will give you the required url for web service.

Share

3 thoughts on “How to dynamically construct the SharePoint site relative url in JavaScript”

Comments are closed.

Share via
Copy link
Powered by Social Snap