Determine the URL of SharePoint Web Services using jQuery/Javascript

If you have read some of the previous posts on this blog related to using jQuery in SharePoint 2007 sites, you probably know that it’s perfectly possible to make call the out-of-the-box SharePoint web services by making use of Javascript running in the client’s browser. This opens up a huge stream of possibilities from which I already covered some of them on my blog. A very important piece of information you need if you want to make a call to a web service is of course the URL of the web service. Figuring out this URL seems to be more trivial than it actually is. My first idea was to use the URL of the page in which the call to a web service actually happened; e.g. you’ve got a Site Page accessible in SharePoint using the URL http://mysite/mypage.aspx, so you strip the /mypage.suffix and add /_vti_bin/lists.asmx (if you’d like to call the Lists web service of course). The thing is, this will only work if your mypage.aspx file is setting in the Root Folder of your SharePoint site. For example: when you would put the Site Page in a Document Library instead, the URL of the page would be http://mysite/Shared Documents/mypage.aspx, so you’d have to strip the /Shared Documents/mypage.aspx and replace it with the Web Service suffix. This can get very complicated when you don’t know upfront in what kind of location the page will be stored (a Site Page in the Root Folder or a Document Library, an Application Page in _layouts, ...). It’s possible to write a bunch of code to figure that out, or you can make use of the following technique!

A very easy and quick way to get a reference is to make use of the alternate link SharePoint will but by default in the head section of every rendered page:

<html><head>
...
<link type="text/xml" rel="alternate" href="/_vti_bin/spsdisco.aspx" />
...
</head>...</html>

The href attribute will always point to a server relative URL of the spsdisco.aspx page; which is located in the same folder as the out-of-the-box SharePoint Web Services (/_vti_bin). Even if you are in a sub site, another site collection, an Application Page, ... the link elemen will always be there, and point to the correct URL. So the following piece of Javascript code, will retrieve the prefix you can use to make calls to the correct URL’s of the SharePoint Web Services:

var spsdiscoUrl = $("head link[rel='alternate']:eq(0)").attr("href");
spWSUrlPrefix = spsdiscoUrl.substr(0, spsdiscoUrl.length - 13);


(If you haven’t already noticed: I’m using the jQuery Javascript library.) The first line will get the value of the alternate link in the head of the HTML page; the second line will strip spsdisco.aspx (13 characters). Once you’ve got this prefix, you can use it to construct the URL of the SharePoint Web Services as follows:

$.ajax({
    url: spWSUrlPrefix + "lists.asmx",
    type: "POST",
    dataType: "xml",
    data: soapEnvTasks,
    complete: processResultTasks,
    contentType: "text/xml; charset=\"utf-8\""
});

UPDATE As Jaap pointed out in the comments, SharePoint creates a Javascript variable called L_Menu_BaseUrl that contains the value needed determine the URL of the SharePoint Web Services. I haven't been able to test it myself, or to figure out who/what is responsible to declare this variable, but it looks promising! The only downside I see related to this approach would be that you rely on the OOB Javascript to be 1) loaded in the page and 2) to have executed before your code is started.

2 Comments

Comments have been disabled for this content.