Cant get SPServices.SPGetCurrentUser to work at all.

May 4, 2012 at 3:01 AM

I'm a bit of a noob when it comes to jQuery, but I can usually get things to work without much of an issue, but I've been trying with this for several hours with no effect.  Here is my code (and yes the src for the JS is correct, that was the first thing I checked.

Also something else for you to consider is that this is being used in a new item form in SharePoint 2010 Foundation.

Thanks ahead of time, please help me.

 

<script language="javascript" type="text/javascript" src="../../../Style Library/Scripts/jquery-1.4.2.min.js"></script>
<script language="javascript" type="text/javascript" src="../../../Style Library/Scripts/jquery.SPServices-0.7.1a.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	var userName = $().SPServices.SPGetCurrentUser({
		fieldName: "Title",
		debug: true
	});
	$("input[Title='FullName']").val(userName);
	
	var userPhone = $().SPServices.SPGetCurrentUser({
		fieldName: "MobileNumber",
		debug: true
	});
	$("input[Title='CellPhone']").val(userPhone);
	
	var userEmail = $().SPServices.SPGetCurrentUser({
		fieldName: "EMail",
		debug: true
	});
	$("input[Title='EMail']").val(userEmail);
});
</script>

May 4, 2012 at 12:49 PM

If you are using SPServices 0.7.1a, you should upgrade your jQuery version to at least 1.7.

 

Cheers,

Matt

May 4, 2012 at 3:41 PM

So what's not working? exactly?

Since you said you were a "noob", lets try a few things to:

  1. Validate the files are being loaded correctly (.js)
  2. Are you getting a a value back from SPGetCurrentUser()
  3. Validate that your code to find the fields on the page are succesfull

Here is a revised piece of code with alerts() inserted to try and send off in the right path:

alert("jQuery loaded? " + (jQuery == undefined ? "NO!!!" : "YES."));
alert("SPServices loaded? " + (jQuery.SPServices == undefined ? "NO!!!" : "YES."));

$(document).ready(function() {
    var userName = $().SPServices.SPGetCurrentUser({
        fieldName: "Title",
        debug: true
    });
    
    alert("SPServices.SPGetCurrentUser(TITLE) returned: " + userName);
    alert("input[Title='FullName'] was found? " + ($("input[Title='FullName']").length > 1 ? "YES." : "NO!"));
    
    $("input[Title='FullName']").val(userName);
    
    var userPhone = $().SPServices.SPGetCurrentUser({
        fieldName: "MobileNumber",
        debug: true
    });
    
    alert("SPServices.SPGetCurrentUser(MOBILENUMBER) returned: " + userPhone);
    alert("input[Title='CellPhone'] was found? " + ($("input[Title='CellPhone']").length > 1 ? "YES." : "NO!"));
    
    $("input[Title='CellPhone']").val(userPhone);
    
    var userEmail = $().SPServices.SPGetCurrentUser({
        fieldName: "EMail",
        debug: true
    });
    
    alert("SPServices.SPGetCurrentUser(EMAIL) returned: " + userEmail);
    alert("input[Title='EMail'] was found? " + ($("input[Title='EMail']").length > 1 ? "YES." : "NO!"));
    
    $("input[Title='EMail']").val(userEmail);
    
});

 

Paul