Get current User Name

Mar 31, 2010 at 1:25 PM
Edited Apr 1, 2010 at 9: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 = "";
$.ajax({
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("table.ms-formtable 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!

Alex

Coordinator
Mar 31, 2010 at 1:54 PM

Alex:

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.

M.

Coordinator
Mar 31, 2010 at 1: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 2:00 PM

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

 

Coordinator
Mar 31, 2010 at 2:16 PM

Try it out: http://spservices.codeplex.com/releases/view/42672

M.

Apr 1, 2010 at 8:59 AM

Marc,

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
$.ajax({
	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("table.ms-formtable 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();

Alex
Apr 1, 2010 at 9:06 AM

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

Apr 1, 2010 at 9:23 AM
Edited Apr 1, 2010 at 9: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
Coordinator
Apr 1, 2010 at 11:31 AM
Edited Apr 1, 2010 at 11:32 AM

Alex:

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.

M.