$().SPServices.SPGetCurrentUser not working on the Title field?

Jul 31, 2013 at 2:46 PM
Hi Experts,

We have a very weird issue that suddenly we cant get the below code working perfectly. Its partially working actually as it was able to populate the InitiatorEmail field without an issue but its not working for the Title field to populate the Initiator field. I have tried replacing the Title by Name and it was able to get the account of the current logged in user. We suspect that its on the script that we are using so we have tried to use the latest JQuery and SPServices scripts but still got the same result. So, we have tried the below set of scripts.

jquery.SPServices-0.7.2.min.js
jquery-1.6.2.js

jquery.SPServices-2013.01.js
jquery-2.0.3.js
<script type="text/javascript" src="/RSQE/SCAR/Scripts/jquery-1.6.2.js"></script>
<script type="text/javascript" src="/RSQE/SCAR/Scripts/jquery.SPServices-0.7.2.min.js"></script>
<script type="text/javascript" language="javascript">

$(document).ready(function() {
    var userName = $().SPServices.SPGetCurrentUser({
        fieldName: "Title"
    });
    $("input[Title='Initiator']").val(userName); 
});</script><script type="text/javascript" language="javascript">


$(document).ready(function() {
    var userEmail = $().SPServices.SPGetCurrentUser({
        fieldName: "EMail"
    });
    $("input[Title='InitiatorEmail']").val(userEmail); 
});</script>
Another thing that we have tried so far is creating a new list and pasting the same code and trying both set of scripts, no difference. However, it works on a newly created Web Application (New Site Collection) but just on the previous JQuery and SPServices scripts (jquery.SPServices-0.7.2.min.js and jquery-1.6.2.js). Any idea or suggestion guys what could be the caused of this issue? This just happens suddenly. We haven't performed any update for the server yet and we already revert all the changes that might have caused this issue but it still persists. Please help.

Thank You,

Arnel
Coordinator
Jul 31, 2013 at 6:01 PM
Arnel:

All the fiddling around with versions of things isn't going to solve this. I'd recommend stepping through the code (or using alerts) to see what you are getting back from the calls to SPGetCurrentUser. You may be getting the right values. Instead it may be that the selectors for the fields you want to set aren't correct.

M.
Aug 1, 2013 at 1:10 AM
Hi Marc,

I put 2 alerts on my code. The first alert displays BLANK value but the second one does displays the email address of the current user. Also, using jquery-2.0.3.js and jquery.SPServices-2013.01.js I don't get any value and alerts at all using the same code below just replacing the reference of the scripts. Have tried re-uploading the scripts using SharePoint Designer but with the same result. We are really stuck now. Please advise.
<script src="/Scripts/jquery-1.6.2.js" type="text/javascript"></script>
<script src="/Scripts/jquery.SPServices-0.7.2.min.js" type="text/javascript"></script>

<script language="javascript" type="text/javascript">

$(document).ready(function() {
    var userName = $().SPServices.SPGetCurrentUser({
        fieldName: "Title"
    }); alert(userName);
    $("input[Title='Initiator']").val(userName); 
});</script><script language="javascript" type="text/javascript">

$(document).ready(function() {
    var userEmail = $().SPServices.SPGetCurrentUser({
        fieldName: "EMail"
    });alert(userEmail);
    $("input[Title='InitiatorEmail']").val(userEmail); 
});</script>
Thanks,

Arnel
Aug 1, 2013 at 1:31 PM
Edited Aug 1, 2013 at 1:33 PM
Why are you defining .ready twice? You only need to use it once, then put any code you want to run on document load inside it.

So your code should be like this:-
<script type="text/javascript" src="/RSQE/SCAR/Scripts/jquery-1.6.2.js"></script>
<script type="text/javascript" src="/RSQE/SCAR/Scripts/jquery.SPServices-0.7.2.min.js"></script>
<script type="text/javascript" language="javascript">

$(document).ready(function() {
    var userName = $().SPServices.SPGetCurrentUser({
        fieldName: "Title"
    });
    $("input[Title='Initiator']").val(userName); 
    
    var userEmail = $().SPServices.SPGetCurrentUser({
        fieldName: "EMail"
    });
    $("input[Title='InitiatorEmail']").val(userEmail); 
    
});
</script>
The way you're doing it will just replace the 1st call you just set up, with the 2nd one, as they're both defining the same function.

You could also use the alternate syntax to just do one call:-
var thisUsersValues = $().SPServices.SPGetCurrentUser({
    fieldNames: ["ID", "Name", "SIP Address"],
    debug: false
});
Coordinator
Aug 5, 2013 at 11:33 AM
ixwood's suggestions are good ones; I'd suggest the latter. No need for multiple calls.

per the documentation, the function really just does some "screen scraping" under the covers, grabbing values from /_layouts/userdisp.aspx?Force=True. I'd suggest looking at that page for the user(s) where you are having troubles and making sure that there are in fact values for the fields you need.

M.