Friday 26 August 2011

JavaScript "L_Menu_BaseUrl" variable for SharePoint

When working with Sandbox Solutions, there are a lot of times when you have to determine the URL or the serverRelativeUrl of the current site. This is especially needed on Office 365.
With the ECMAScript Client Object Model, there are multiple ways to access the current site URL or the serverRelativeUrl like the SP.Site.get_url() for the Root site URL or SP.Web.get_serverRelativeUrl() for the relative url of the current site.

But there is an easier way get the URL  by using the OOB Javascript variable L_Menu_BaseUrl which stores the URL of the current Web or the Subsite.

For example, this was extremely helpful to me for calling the EditDialog on any list item. It worked great with Office 365 top level sites as well as sub-sites:

function OpenEditDialog(itemid)
{
   var options = {

    url: L_Menu_BaseUrl + "/Lists/Claims/EditForm.aspx?ID=" + itemid + "&IsDlg=1",
     width: 700,
     height: 700,
     dialogReturnValueCallback: DialogCallback 
};
     SP.UI.ModalDialog.showModalDialog(options);
}

Here are some more OOB Javascript variables which can be useful: 

L_Menu_LCID : This contains the LCID setting of the current site.
L_Menu_SiteTheme : This contains the theme name of the current site.
_spUserId : This contains the id of the current user.