This project has moved and is read-only. For the latest updates, please go here.

Get current User Name

Mar 31, 2010 at 2:25 PM
Edited Apr 1, 2010 at 10:24 AM

I have been using SPGetCurrentUser with lots of success (thanks). I have found occasions when I need the display name of the current user (SPGetCurrentUser returns the login name). So I created a new method based on SPGetCurrentUser but with one subtle tweak - look for the internal field "Title" instead of "Name":


// Function which returns the account USER name for the current user in display name format
$.fn.SPServices.SPGetCurrentUserName = function() {
var username = "";
async: false, // Need this to be synchronous so we're assured of a valid value url: $().SPServices.SPGetCurrentSite() + "/_layouts/userdisp.aspx?Force=True", complete: function (xData, Status) {
$(xData.responseText).find(" td#SPFieldText").each(function() {
if(/FieldInternalName=\"Title\"/.test($(this).html())) username = $(this).text();
return username.replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');


I hope this is helpful for someone!


Mar 31, 2010 at 2:54 PM


Good stuff!  I'm thinking that, rather than having a separate function, I could just add an option so that you could pass in the value you want from SPServices.SPGetCurrentUser.  Since the function just grabs the user profile page, you should be able to grab any value on it that you want.


Mar 31, 2010 at 2:55 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Mar 31, 2010 at 3:00 PM

Yeah, that would be a great improvement - make it a more generic function to get any value from the User Information page.


Mar 31, 2010 at 3:16 PM

Try it out:


Apr 1, 2010 at 9:59 AM


I got an error on line 1353:

if(thisTextValue.test($(this).html())) {

This is beacuse the "test" function is not available on a string. My fix is to create a RegExp object from the string, and use that to test:

var thisTextValue = "FieldInternalName=\"" + opt.fieldName + "\"";
var thisRegExp = new RegExp(thisTextValue); // Need to create a new RegExp from the string
	async: false, // Need this to be synchronous so we're assured of a valid value
	url: $().SPServices.SPGetCurrentSite() + "/_layouts/userdisp.aspx?Force=True",
	complete: function (xData, Status) {
		$(xData.responseText).find(" td#SPFieldText").each(function() {
			if(thisRegExp.test($(this).html())) { // need to use the RegExp object here (.test not available on a string object)
				thisField = $(this).text();

Apr 1, 2010 at 10:06 AM

I see that a new alpha version was released with a RegExp fix... good job!!

Apr 1, 2010 at 10:23 AM
Edited Apr 1, 2010 at 10:34 AM

It might be worth showing what the Field Name (shown on screen) mappings to Field Internal Names are for the available fields on the user information page - especially as Name, Title and Job Title can get confusing. I extracted these from the source HTML (thanks to MS they were neatly commented!!):

Field Name Field Internal Name
Account Name
Name Title
Work e-mail EMail
About me Notes
Picture Picture
Department Department
Title JobTitle
SIP Address SipAddress
First name FirstName
FirstName LastName
Work phone WorkPhone
Office Office
User name UserName
Web site WebSite
Responsibilities SPResponsibility
Apr 1, 2010 at 12:31 PM
Edited Apr 1, 2010 at 12:32 PM


Thanks for neatly formatting this for the docs. ;+)  I won't update the docs until I release v0.5.4, but I'll add this table to the Release Notes now.

Sorry about that bum ALPHA3. I did it too fast and didn't test.  What's there now should be good to go.