Bug in SPGetCurrentUser when in root site (SP2013)

Jul 3, 2013 at 10:02 PM
I think that I've discovered a bug in SPGetCurrentUser when in a site which is at the root of the web server e.g. http://mydomain/SitePages/Home.aspx. The relevant code is:
var thisWeb = opt.webURL.length > 0 ? opt.webURL : $().SPServices.SPGetCurrentSite();
// Get the UserDisp.aspx page using AJAX
$.ajax({
        // Need this to be synchronous so we're assured of a valid value
        async: false,
        // Force parameter forces redirection to a page that displays the information as stored in the UserInfo table rather than My Site.
        // Adding the extra Query String parameter with the current date/time forces the server to view this as a new request.
        url: thisWeb + "/_layouts/userdisp.aspx?Force=True&" + new Date().getTime(),
        complete: function (xData) {
                thisUserDisp = xData;
        }
});
When at the root, thisWeb = "/", so the url for the ajax call becomes
//_layouts/userdisp.aspx?Force=True&
Notice the double slash at the start, which is obviously nonsense.

To resolve this I added the following after the line to get thisWeb

if (thisWeb == "/")
{
thisWeb = "";
}

and it then worked fine.

Apart from that, great project.

Richard
Jul 4, 2013 at 12:34 AM
Richard,
+1 for "looking under the covers" :)

But....
Just curious. Are you setting webURL parameter on input to / ?

I'm assuming you are based on the code snippet above. If that is the case, then I don't believe this is a bug...

webURL should be a valid value that gets you to the root of the desired Site (example: you should be able to paste it into a browser address bar and go to that site).
Using your example, I think it should be the following: http://mydomain

_____
Paul

Sent from mobile device.
Jul 4, 2013 at 7:20 AM
No, we're not setting the webURL at all. Full code is:
<script src="/SiteAssets/jQuery/jquery-1.10.1.min.js" type="text/javascript"></script>
<script src="/SiteAssets/jQuery/jquery.SPServices-2013.01.js" type="text/javascript"></script>

<script type="text/javascript">
 $(document).ready(function(){
 var currentUserDetails = $().SPServices.SPGetCurrentUser({
 fieldNames: ["ID", "Name"],
 debug: true
 });

 alert(currentUserDetails['ID']);
 alert(currentUserDetails['Name']);

 });
 </script>

Jul 4, 2013 at 3:31 PM
Thanks Richard....

Could you place the following alert prior to calling SPGetCurrentUser:
alert("webURL: " + $.fn.SPServices.SPGetCurrentSite() );
Let us know if this return a '/' or the full URL (http://....)
I'm still thinking this is not being cause (explicitly) by SPServices, but rather how the server is perhaps configured...

Paul.
Jul 4, 2013 at 5:40 PM
Definitely a bug.

$.fn.SPServices.SPGetCurrentSite() returns "/"

I've tracked it down further and ultimately it's set in the SPServicesContext to _spPageContextInfo.webServerRelativeUrl. So it's always a server relative url.
    function SPServicesContext() {
    
        // SharePoint 2010 gives us a context variable
        if(typeof _spPageContextInfo !== "undefined") {
            this.thisSite = _spPageContextInfo.webServerRelativeUrl;
            this.thisList = _spPageContextInfo.pageListId;
            this.thisUserId = _spPageContextInfo.userId;
        // In SharePoint 2007, we know the site and UserId
        } else {
            this.thisSite = (typeof L_Menu_BaseUrl !== "undefined") ? L_Menu_BaseUrl : "";
            this.thisList = "";
            this.thisUserId = (typeof _spUserId !== "undefined") ? _spUserId : undefined;
        }
        
    } // End of function SPServicesContext
Richard
Jul 4, 2013 at 5:47 PM
Going to have the same issue in another couple of places where SPGetCurrentSite is called.
Jul 4, 2013 at 6:00 PM
Richard,
Yeah, see what you mean... Marc's reusing the information already available/provided by SP to the browser and in this case, he may have to adjust "thisSite" if it detects that its the root site... specially since the assumption is that webURL value does not end with a /

Again, just for curiosity, could you run this and see what you get back (I guess you will need to run this with your proposed fix in place in order for it to work in your env):
(function(){
    // example taken from: https://spservices.codeplex.com/wikipage?title=WebUrlFromPageUrl
    $().SPServices({
      operation: "WebUrlFromPageUrl",
      pageURL: ((location.href.indexOf("?") > 0) ? location.href.substr(0, location.href.indexOf("?")) : location.href),
      completefunc: function (xData, Status) {
        $(xData.responseXML).SPFilterNode("z:row").each(function() { 
           alert( "webURL is: " + $(xData.responseXML).find("WebUrlFromPageUrlResult").text() );
      }
    });  
})();
Still interested in seeing if the server returns the full url of the site or a relative one...

Paul
Jul 4, 2013 at 8:47 PM
Can't get that to work at all. I thinks the syntax is wrong somewhere.
Jul 4, 2013 at 9:06 PM
Thanks Richard.

I'm sure Marc will comment on here once he has had a chance to look at this.

_____
Paul

Sent from mobile device.
Coordinator
Jul 9, 2013 at 5:22 AM
It sounds like I may have a bug. I'll copy this into an item in the Issue Tracker and take a look at it in my next pass.

M.
Coordinator
Jul 9, 2013 at 5:23 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.