Questions About $().SPServices.SPGetCurrentUser

Coordinator
Oct 1, 2010 at 1:35 AM
mrSPARQLE Today at 12:43 AM 
Hi, I'm able to return the "Name" field but any other field I query returns the same value (the account name). I've checked the "/_layouts/userdisp.aspx?Force=True" page and the fields I'm trying to return are visible. Seems like the input is being ignored/erroring and the default value is being returned. Any ideas as to what might be causing this?

sympmarc Today at 1:41 PM 

Are you specifying the Field Internal Name? M. 
 
mrSPARQLE Today at 8:40 PM
Hi Marc, I am specifying the Field Internal Name like so:
var userJobTitle = $().SPServices.SPGetCurrentUser({
fieldName: 'JobTitle',
debug: false
});

is that correct?
Coordinator
Oct 1, 2010 at 1:36 AM

Yes, that looks right. I just tested in my environment with JobTitle, SIPAddress, etc., and it seems to be working fine. I wonder what's different on your end. What version of SPServices are you using?

M.

Oct 3, 2010 at 11:36 PM

I updated to the latest versions so I'm now running 0.5.6 of SPServices and 1.4.2 of the JQuery library, but still getting the same error I'm afraid.

Coordinator
Oct 3, 2010 at 11:47 PM
Can you show me the code? M.
Oct 4, 2010 at 2:20 AM

Sure, here goes:

<script type="text/javascript" src="http://sp/Resources/jquery.SPServices-0.5.6.min.js"></script>
 <script type="text/javascript" src="http://sp/Resources/jquery-1.4.2.min.js"></script>  
 
 <script type="text/javascript">

 $(document).ready(function(){
  var userLogin = $().SPServices.SPGetCurrentUser({
  fieldName: 'Name',
  debug: true  
  });
  
  var userFullName = $().SPServices.SPGetCurrentUser({
  fieldName: "Title",
  debug: true 
  });
  
  var userJobTitle = $().SPServices.SPGetCurrentUser({
  fieldName: "JobTitle",
  debug: false
  });  
  
  var userEmail = $().SPServices.SPGetCurrentUser({
  fieldName: 'EMail',
  debug: false  
  });

         $('#login').text(userLogin.toString());  
  $('#name').text(userFullName.toString());
  $('#jobTitle').text(userJobTitle.toString());
  $('#email').text(userEmail.toString())
 
    });

</script>
<div>
 <table>
  <tr>
   <td><p>login: </p></td>
   <td><p id="login"></p></td>
  </tr>
  <tr>
   <td><p>name: </p></td>
   <td><p id="name"></p></td>
  </tr>
  <tr>
   <td><p>job title: </p></td>
   <td><p id="jobTitle"></p></td>
  </tr>
  <tr>
   <td><p>email: </p></td>
   <td><p id="email"></p></td>
  </tr>
 </table>
</div>
    

And the returned values are just my username in each row. Thanks for your time on this, Marc

Coordinator
Oct 4, 2010 at 3:09 AM
Edited Oct 4, 2010 at 4:05 AM

I just dropped your code verbatim into my environment and I get the results we'd expect.

The one thing which is wrong with what you've posted above is that the reference to SPServices is before the jQuery reference. In that order, I just get an error, no results. This is because jQuery isn't defined when SPServices is loaded.

M.

Oct 4, 2010 at 4:10 AM

I switched the references and got the same result. There must be something different about my environment so I can try to attack it from that direction now. Thanks for your help, Marc.

Oct 4, 2010 at 4:37 AM

Your advice got me thinking about why I wasn't getting an error even though my references were around the wrong way and as it happens, there was a reference to older verions of the JQuery and SPServices libraries in another web part. Thanks again.

Coordinator
Oct 4, 2010 at 11:11 AM

Earlier versions of SPGetCurrentUser *only* returned the username, so that makes sense. So it's working?

M.

Oct 4, 2010 at 10:44 PM

Oops, guilty of overlooking the obvious on that one. Yeah, it's working perfectly now. Cheers.

Mar 31, 2011 at 9:16 PM

According to the documentation I should be able to retrieve custom fields, but it does not appear to be working? I have a custom field "Organization" which appears in my user profile and has data, but I get nothing from the SPGetCurrentUser call. Standard fields appear to work fine. Am I missing something? Relevant code snippet is:

    var agency = $().SPServices.SPGetCurrentUser({
            fieldName: "Organization"
        });

Thanks!

Chris

Apr 1, 2011 at 3:38 AM

For your custom field to work, it'll have to render in the DOM w/ attributes SPServices looks for.  Here is a snippet from the source code:

			url: $().SPServices.SPGetCurrentSite() + "/_layouts/userdisp.aspx?Force=True&" + new Date().getTime(),
			complete: function (xData, Status) {
				$(xData.responseText).find("table.ms-formtable td[id^='SPField']").each(function() {

This code looks at the userdisp.aspx page and then parses through the xml using the selector: table.ms-formtable td[id^='SPField'].  So for your custom field to work correctly with SPServices, 

these attributes must be present in the DOM, so the code can filter/find the information accordingly.  

 

Does that help?

Apr 7, 2011 at 1:36 PM

Will this javascript work to get SP Username in a Java Servlet?  We migrated from Oracle Webcenter to MOSS portal and several java applications need to be moved to MOSS, they only need current logged in user.

Coordinator
Apr 7, 2011 at 2:43 PM

jtblak2:

I don't think I can really answer that. The script ought to work, but the authentication will have to be right. I'd set up a small test harness to see if it works.

M.

Jul 5, 2011 at 4:20 PM

Hi.

I'm getting the same error as mrSPARQLE. I've been trawling the net all afternoon fopr solutions. My system is running:

MOSS 2007 stnd version 12.0.0.6421
jquery 1.6
SPServices 0.6.2

here's the code

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.min.js"></script>
<script type="text/javascript" src="/SiteCollectionDocuments/jquery.SPServices-0.6.2.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {

    var FName = $().SPServices.SPGetCurrentUser({fieldname: 'FirstName', debug: false});
    var LName = $().SPServices.SPGetCurrentUser({fieldname: "LastName", debug: false});
       
    $("input[Title='Loaned By IT Staff Memeber']").val(FName + ' ' + LName);
   
});
</script>

All that is being returned in the textbox id domain\username twice seperated by a space

Any help would be greatly appreciated :-)

Coordinator
Jul 5, 2011 at 4:40 PM

I explain the way that SPGetCurrentUser works on the doc page, but basically, it uses AJAX to grab /_layouts/userdisp.aspx and "scrapes" the values from the page.

I just tried your code on my WSS instance, and I see the DOMAIN\name coming back for both FirstName and LastName. I think that's a bug. However, are you sure you are running MOSS and not WSS? FirstName and LastName are only available for MOSS, not WSS.

M.

Coordinator
Jul 5, 2011 at 4:47 PM

Aha! It's the little things. No bug, just a typo in your code. It needs to be "fieldName".

   var FName = $().SPServices.SPGetCurrentUser({fieldName: "FirstName", debug: false});
   var LName = $().SPServices.SPGetCurrentUser({fieldName: "LastName", debug: false});
   alert(FName + "::" + LName);

M. 

Jul 6, 2011 at 8:23 AM

Aghhhh. curse of the bloody typo......

I feel reyt daft now. Cheers for that :-D

A.

Coordinator
Jul 6, 2011 at 11:07 AM

Hey, we all do it, all the time. Glad it was a simple fix.

M.

Sep 7, 2011 at 5:47 PM

I really need to use this with a custom attribute.  I saw the post above, something about using the DOM and some custom code, but I really don't know how to do that (where the code goes).  Here's the code I'm trying to use...it would work if "datatelID" were an attribute that jQuery could pull. 

 

<script language="javascript" type="text/javascript" src="/Documents/jquery-min.js"></script>
<script language="javascript" type="text/javascript" src="/Documents/jquery.SPServices.js"></script>
<script type="text/javascript">  
 $(document).ready(function(){ 
  var userEmail = $().SPServices.SPGetCurrentUser({
  fieldName: 'datatelID',
  debug: false   
  });
  $('#datatelID').text(userEmail.toString())
  
    });
</script>
&nbsp;<a id="datatelID">

Coordinator
Sep 9, 2011 at 3:48 PM

mqudites:

It all depends where the "custom attribute" is stored. I would expect that it's a part of the User Profile, so this function won't help. You should look at the User Profile Service.

M.

Sep 9, 2011 at 3:54 PM
sympmarc wrote:

mqudites:

It all depends where the "custom attribute" is stored. I would expect that it's a part of the User Profile, so this function won't help. You should look at the User Profile Service.

M.


The custom attribute is part of the profile...it's just a custom one that I created, separate from the Microsoft-supplied ones.

I took a look at that page, but honestly I don't understand how I would use it.

Coordinator
Sep 10, 2011 at 4:44 PM

mqudites:

Check out the main documentation page. At the bottom, I give the basic steps to use SPServices. Post back in a new thread if you need help, but you'll need to do some work on your end to understnad how the Web Services work.

M.

Sep 12, 2011 at 2:06 PM

Marc,

I’m actually already using SPServices within Sharepoint, to pull a different field that jQuery does natively support. What I’m trying to do is get it to pull a field that it doesn’t natively support…that’s where I’m getting stuck.

For example, here’s how I’m already utilizing it. I want to do the same thing, except pull a different, custom field:

<script type="text/javascript">

$(document).ready(function(){

var userEmail = $().SPServices.SPGetCurrentUser({

fieldName: 'EMail',

debug: false

});

$('#email').text(userEmail.toString())

});

</script>

Coordinator
Sep 12, 2011 at 2:09 PM

Again, if that field is a User Profile field, then you need to use the Web Service to get the value. The SPGetCurrentUser only returns the information shown in the documentation, which comes from the User Information List.

M.

Sep 27, 2011 at 4:05 PM

Hello ,

 

Im using the" $().SPServices.SPGetCurrentUser({" function to obtain the name of the "department"  to create views for a system , it works fine with the administrators users  , but the regular users , it doesnt, any sugestions ??

Sep 27, 2011 at 4:34 PM

There are a couple of permissions that users will need to have for this to work. Normally they would exist out of the box, but they may have been changed. I have no access to the full permissions list on my site so I have to throw out the ones I think you should check: (In Order of Importance)

View Pages (I think this is the biggest one. I found it disabled and this function did not work), Edit Personal User Information, Enumerate Permissions, Browse User Information

 

Sep 27, 2011 at 4:56 PM

i gave them all the rights and still didn´t work .

Sep 28, 2011 at 10:14 AM

The View Pages permission is what allows users to see the _layouts directory where the function is pointing the user. If View Pages is selected and they do not have a more limiting permission applied, then you could give them read access to the User Information List for the site collection. I have had to do this in WSS. I would either give Basic Users or Authenticated Users read only access to the list.

Dec 20, 2011 at 6:59 PM

This looked like the perfect code snippet to grab the Current User ID... except for some reason the code isn't returning anything aside from the field headings. I can easily access "_layouts/userdisp.aspx?Force=True" and retrieve all my info, so I don't think it's any sort of a permissions issue. Plus I'm an administrator and I've opened the User Information List for viewing.

Any ideas? Thanks!

<script type="text/javascript" language="javascript" src="../jQuery/jquery-1.7.1.min.js"></script>
<script type="text/javascript" language="javascript" src="../jQuery/jquery.SPServices-0.7.0.min.js"></script>

<script type="text/javascript">

$(document).ready(function(){
 
  var userName = $().SPServices.SPGetCurrentUser({
  fieldName: 'Name',
  debug: true   
  });
  
  var userFullName = $().SPServices.SPGetCurrentUser({
  fieldName: "Title",
  debug: true  
  });
  
  var userID = $().SPServices.SPGetCurrentUser({
  fieldName: "ID",
  debug: true
  });  
  
  var userEmail = $().SPServices.SPGetCurrentUser({
  fieldName: 'EMail',
  debug: true   
  });

  $('#userName').text(userName.toString());  
  $('#name').text(userFullName.toString());
  $('#ID').text(userID.toString());
  $('#email').text(userEmail.toString())
  
});

</script>

<div>
 <table>
  <tr>
   <td><p>userName: </p></td>
   <td><p id="userName"></p></td>
  </tr>
  <tr>
   <td><p>name: </p></td>
   <td><p id="name"></p></td>
  </tr>
  <tr>
   <td><p>userID: </p></td>
   <td><p id="userID"></p></td>
  </tr>
  <tr>
   <td><p>email: </p></td>
   <td><p id="email"></p></td>
  </tr>
 </table>
</div

Coordinator
Dec 20, 2011 at 7:02 PM

I would add some alerts into your script to see that the values contain. It looks like you are doing everything right, but it's always difficult to debug from afar.

M.

Dec 20, 2011 at 7:09 PM

I saw your post @ http://spservices.codeplex.com/discussions/283523 and even tried using single quotes... and the alert. The alert does pop up, but it's blank.

Coordinator
Dec 20, 2011 at 7:12 PM

Next, you should try stepping through the script in Firebug or your tools of choice. You haven't shown where you put the alert, but I'd also make sure that your references to the script files are correct.

M.

Dec 20, 2011 at 7:22 PM

ok... the alert is now working, but only when I call the ID variable. If I choose any other Field Internal Name, it pulls up blank.

This is the whole code I'm using now for test:

<script type="text/javascript" language="javascript" src="../jQuery/jquery-1.7.1.min.js"></script>
<script type="text/javascript" language="javascript" src="../jQuery/jquery.SPServices-0.7.0.min.js"></script>

<script type="text/javascript">

$(document).ready(function(){
 
alert($().SPServices.SPGetCurrentUser({fieldName: 'ID'}));
 
});

</script>


Coordinator
Dec 29, 2011 at 1:33 PM

netdirector:

I just tested your script in my dev environment using the Developer Tools in IE9, and it seems to work fine for Name, Title, and EMail. I'm not sure what the issue is.

M.