Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I was working on a project where I needed to get the current web's ID or URL (sometype of unique identifier) in javascript without making an service call (executeQueryAsync). I didn't seem to be able to find a method that would return this information before a load and executeQueryAsync. Through IE dev tools I was able to find the value of the url and the ID in the object but it was stored in some strang variables ($0_0.$1P_0 and $0_0.$r_0[0].$M_1). Does anyone know of a supported way to get this information before the web object is loaded through the executeQueryAsync. Here's some code to refer to, I basically don't want to perform the extra service call for this website if I've already done it once in this session for this website.

function DoStuff() {
    if ( GetCookie("MyStuff"+SP.ClientContext.get_current().get_web().$0_0.$1P_0) ) return;

    var listTitle = "Stuff";

    var context = new SP.ClientContext.get_current();
    this.web = context.get_web();
    this.user = web.get_currentUser();
    this.list = web.get_lists().getByTitle(listTitle);

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Author' /><Value Type='Integer'><UserID /></Value></Eq></Where></Query><RowLimit>1</RowLimit></View>");
    this.listItems = list.getItems(camlQuery);

    context.load(web);
    context.load(user);
    context.load(list);
    context.load(listItems);

    context.executeQueryAsync(StuffSucceeded, StuffFailed);
}

function StuffSucceeded(sender, args) {
    DoSomeStuff();
    SetCookie("MyStuff" + SP.ClientContext.get_current().get_web().$0_0.$1P_0, 'True', '/');
}

function StuffFailed(sender, args) {} 

ExecuteOrDelayUntilScriptLoaded(DoStuff, "sp.js");
share|improve this question

2 Answers 2

up vote 3 down vote accepted

You can get the server-relative web url from anywhere in SharePoint by accessing a variable called L_Menu_BaseUrl. If you view source on ant page in SharePoint you'll find it defined.

share|improve this answer
    
Thanks, I unfortunately never knew about that. I have now found this page which has a few more listed: ruudheemskerk.net/archive/2010/08/03/… –  Steve Lineberry Apr 26 '12 at 16:03
    
L_Menu_BaseUrl is also available for SharePoint 2013. That's great and thousands of thanks to Derek Gusoff! –  FrankH Jul 2 '13 at 9:48

For SharePoint 2010 I'd recommend using _spPageContextInfo.webServerRelativeUrl

share|improve this answer
    
Good Info, thanks! –  Steve Lineberry Apr 26 '12 at 22:42
    
Sometimes _spPageContextInfo is null. I found L_Menu_BaseUrl to be safer or even var context = new SP.ClientContext(); var relativeWebUrl = context.get_url(); –  Dennis G May 12 at 15:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.