Blog Stats

Determining the Current Site URL with jQuery and SharePoint’s Web Services

I’m not sure that I’m enamored with this as a solution, but I thought that I’d post this function to see what folks think and whether there are any better ideas out there. (I’ve looked, and I certainly can’t find any!) I’m building this function to sit in our SharePoint jQuery library, so I can’t use SPContext, etc.

The basic idea is this:

Take a stab at the current site’s URL by substringing the current location.href

Call the GetWeb Web Service to see if that succeeds. The trick is that we need to know the current site’s URL to build the right URL for the Ajax call to the Web Service in the first place.

If the call fails, then trim off the last URL segment and try again.

This may also have cross-browser issues; I’m still only working in IE just to get things up and running first.

UPDATE 2009-08-20: Much better solution – WebUrlFromPageUrl operation. This operation returns the webUrl for any page’s URL. We’ve implemented this in our jQuery library for SharePoint Web Services as of version 0.2.4.

10 Comments

[a few minutes later] I scanned the source code of a Web page, in both SP 2007 and SP 2010, and there’s a global variable that stores the site relative URL: L_Menu_BaseUrl. A search confirmed this:http://www.google.com.hk/search?q=L_Menu_BaseUrl

I’m not sure that I have a rational reason for it, but I’ve shied away from using most of the JavaScript variables which SharePoint creates in pages. There’s zero documentation and I just never trusted that they would always be there. Call me overly cautious. Of course, the methods I choose to use might be equally prone to issues, but at least I know how they work!

I expected this answer :-) Your choice makes sense, and this is one of the reasons why SPservices is a robust solution.
As for me, I am willing to take some risks, especially as the variables work in all situations (including anonymous access). Maybe I am too optimistic, but my rule of thumb is that a variable is reliable if it’s been carried over from SP2007 to SP2010.

You’re probably right. I was just looking for such a variable today which would tell me when a BusinessDataListWebPart was finished loading. It was for a client page, not SPServices, but still. Any ideas?

Thanks for the reply Mark.
Actually I have a got a link in the top navigation which opens up a new item form of a list in a dialog box. What I want is to get the url of a page from where this link has been opened? For e.g. If I click the link from http://sitename/lists/test then I will need this url or if I click the link from http://sitename/site/lists/test then I will need this url. So I need to know the location from where this link has been clicked.
Thanks Again.

Think you’ve got great Active Directory data?

Try the FREE Hyperfish Analyzer
Find out just how bad your Active Directory data really is. In your custom report you will see how you are faring with key attributes including Profile Pictures, Phone Numbers, and Job Titles, as well as learn the value of an up to date Directory.