Function $().SPServices.SPGetCurrentUser not working

Sep 10, 2013 at 3:55 PM
Hi.

I am new in developing in Sharepoint and I want to get the UserID of the user who is loged in. This UserID i want to put at the end of a hyperlink, so that it is a dynamic hyperlink for every user. But if i run the following code, I only get an alert("hello") which is at the top of the script. everything after that, is not running.
I put this code in a masterpage of Sharepoint and it doesn't work.
I am using Sharepoint 2013 and SPServices 2013.01. The JQuery Version is 1.8.3.

Here is the Code i am using:
<script src="JavaScripts/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="JavaScripts/jquery.SPServices-2013.01.min.js" type="text/javascript"></script>
<script type="text/javascript">
alert("hello");             
var thisUserAccount = $().SPServices.SPGetCurrentUser({fieldName: "ID", debug: false});
alert(thisUserAccount);
var link = document.getElementById("myDocuments");
alert("[URL]" + thisUserAccount);
                                
link.href = "[URL]/" + thisUserAccount;
                            
var link2 = document.getElementById("myTasks");
link2.href = "[URL]" + thisUserAccount + "AllTasks.aspx";
                            
</script>
thank you for your help!
Coordinator
Sep 10, 2013 at 4:00 PM
Odds are the references to jQuery and/or SPServices aren't correct. My guess is that you are getting an error about $ or SPServices not being defined.

You should also look at using $(document).ready(), as the DOM elements you want to manipulate may not be present in the page when your script runs.

M.
Sep 11, 2013 at 2:52 PM
If all you want is to get the ID, then you don't have to go through SPServices... That information is already available on the page - SP gives it to you... the global variable name is:
_spUserId
or
_spPageContextInfo.userId
This should take care of actually getting the ID... As mark indicated, this sounds like you don't have the references to jQuery correct... Remember that if you placed the code above in the master page, it may not work on all pages because your references to jQuery and SPServices are relative (to the MASTER page)... since the master page gets applied to different pages through out a site, those links will be invalid...

You should use absolute links or links that flow off the Site's root... I can't remember now, but I think there are a few variables you can use on master pages to do this... things like ~Site or $SPUrl... I did a quick search and found this: http://techizs.blogspot.com/2012/02/site-collection-url.html

To test that the references are correct, place the following in your alert:
alert("Executing... jQuery: " + $.fn.jquery + " , SPServices: " + $.fn.SPServices.Version() );

Paul.
Sep 13, 2013 at 10:41 AM
It's working!
You were right Marc. The references didnt' work.

Paul thank you very much. Your examples helped me very much.
The _spPageContextInfo.userId worked, but i searched the UserName. I have written it wrong in my post.
But you helped me a lot with your post!
Sep 14, 2013 at 1:43 PM
Glad it worked out.
If you had to so something different in your master page, you should post the code you used to reference the libraries. It will help others that land here in the future looking for help.
Paul.


--

_________
Paul T