Quantcast
Viewing all articles
Browse latest Browse all 62

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&quot;);">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.


Viewing all articles
Browse latest Browse all 62

Trending Articles