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.
var thisSite = location.href.substring(0, location.href.lastIndexOf('\/')); $.fn.SPServices.SPGetCurrentSite = function() { ... $.ajax({ async: false, // Need this to be synchronous so we're assured of a valid value url: thisSite + "/_vti_bin/Webs.asmx", beforeSend: function (xhr) { xhr.setRequestHeader("SOAPAction", "<a href="http://schemas.microsoft.com/sharepoint/soap/GetWeb");">http://schemas.microsoft.com/sharepoint/soap/GetWeb");</a> }, type: "POST", data: msg, dataType: "xml", contentType: "text/xml; charset=\"utf-8\"", success: function (xData, Status) { $(xData.xml).find("Web").each(function() { thisSite = $(this).attr("Url"); }); }, error: function (XMLHttpRequest, textStatus, errorThrown) { thisSite = thisSite.substring(0, thisSite.lastIndexOf('\/')); $().SPServices.SPGetCurrentSite(); } }); return thisSite; };
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.
Looking into this right now, there must be an easier way! (but I can’t find it either).
[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
Christophe:
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!
M.
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?
M.
Hi Mark,
Thanks for this article and spservices. Is there any way to get the full url of a page rather than just the site name with spservices or any other method?
Thanks again
KK:
Do you mean location.href?
M.
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.
KK:
The more “SharePointy” way to do this is to pass the Source parameter on the URL. That way the form returns to that location when you save or cancel. It’s built in functionality.
So some thing like (typing from memory – not tested):
M.
Thanks again Mark. I’m quite close now.