2

I want to get the log-in name of user from people picker control using jquery.

I have a code which displays the display name of the user but I am not able to get the login name.

Code is as follows:

$(document).ready(function () {
$("input[title='Project Owner']").focusout(function () {    

    var loginName = $("span.ms-entity-resolved").attr("title");

    alert("Login Name:" + loginName);

    var displayName = $("span.ms-entity-resolved div:first-child").attr("displaytext");

    alert("Display Name :" +displayName);

});

});

Here Project Owner is name of the PeoplePicker control.

How do I get the login name?

users1100
  • 3,230
  • 6
  • 61
  • 114

4 Answers4

1

I did some tricks and found the solution finally, I don't know it is the optimal solution but it works in my case.

The code is here:

$(document).ready(function () {
$("input[title='Project Owner']").focusout(function () {    

    var loginName = $("span.ms-entity-resolved").attr("ID");

    var start = loginName.indexOf(":");

    var end = loginName.indexOf("_Processed");

    var finalUserName = loginName.substring(start-1, end);

    alert(finalUserName);
});


});
users1100
  • 3,230
  • 6
  • 61
  • 114
0

Actually it is alot easier - You can access a normal peoplepicker in SP2013 the same way you would access a clientside one.

The Problem is, that you need to know the ID to identify the peoplepicker. Alternatively you can iterate over the global Object containing the PeoplePicker's.

If you check out the SPClientPeoplePicker.SPClientPeoplePickerDict object in your debugging console, there are all peoplepicker of this page stored in it (iterate here to get all peoplepicker).

enter image description here

You can access the entered user's information like this:

SPClientPeoplePicker.SPClientPeoplePickerDict[yourPickersID].GetAllUserInfo()

Example User Now every object has stored the properties of an user that was entered in the picker.

enter image description here

Here's an example for a group Example Group

Mx.
  • 3,323
  • 1
  • 20
  • 41
0
var ctx = new SP.ClientContext();
var user = ctx.get_web().get_currentUser();
ctx.load(user);
ctx.executeQueryAsync(function() {
    alert(user);
});​

above is using sharepoint javascript object model sp.js, you cant do it the way your doing it in your example!

Getting the username from javascript client object model?

also look here:

Get current user in Client Object Model with javascript?

EDIT

sorry i missunderstood your question!

$(document).ready(function() 
{ 
     $("input[title='JM ID']").focus(function()   
     { 
          var User = $("textarea[title='People Picker']").val(); 
          alert(User);
     }); 
}); 

that should get the user name from people picker!

http://ronsp.wordpress.com/2010/01/19/get-value-from-people-picker-using-jquery/

EDIT

strange should of worked! have you tried the full code?

$(document).ready(function() 
{ 
   $("input[title='JM ID']").focus(function()   
   { 
      var User = $("textarea[title='People Picker']").val().split("\\"); 
      alert(User);
   }); 
}); 

or

$(document).ready(function() 
{ 
   $("input[title='JM ID']").focus(function()   
   { 
      var User = $("textarea[title='People Picker']").val().split("\\"); 
      alert(User[0]);
      alert(User[1]);
   }); 
});
Ali Jafer
  • 17,808
  • 1
  • 27
  • 41
  • Will it get the current logged in user or the user that is entered in the PeoplePicker? – users1100 Nov 01 '13 at 10:51
  • just ammended my answer – Ali Jafer Nov 01 '13 at 10:59
  • I tried the code and I am getting undefined in the alert box :( – users1100 Nov 01 '13 at 11:02
  • what version of sharepoint are you using? – Ali Jafer Nov 01 '13 at 11:13
  • Sharepoint 2013... – users1100 Nov 01 '13 at 11:14
  • Is not possible with SP2013? – users1100 Nov 01 '13 at 11:29
  • it is possible! the above should of worked! make sure that user varible is populated or called properly like "textarea[title='People Picker']" is correct on your side! – Ali Jafer Nov 01 '13 at 11:46
  • I tried the same code you provided in update but I am getting no alert box. It just loses focus on next control. Here is my code: `$(document).ready(function () { $("input[title='Project Owner']").focusout(function () {
        //var loginName = $("span.ms-entity-resolved").attr("title");
    
        //alert("Login Name:" + loginName);
    
        var user = $("textarea[title='People Picker']").val().split("\\");
    
        alert(user[0]);
    
    });
    
    

    });`

    – users1100 Nov 01 '13 at 11:48
  • While testing the code I am entering single user value, but no alert box. :( – users1100 Nov 01 '13 at 11:49
  • how about alert( $("textarea[title='People Picker']").val()); what do you get from that only? – Ali Jafer Nov 01 '13 at 11:51
  • The alert box comes up with undefined.. – users1100 Nov 01 '13 at 11:53
  • ok, you could follow this! its for 2013 as i think the val in the above method is wrong... I dont have 2013 machine atm to test what val it is in html! http://www.sharepointcolumn.com/sp2013-setting-people-picker-value-in-newform-aspx/ – Ali Jafer Nov 01 '13 at 12:04
  • Ok, but I got that link before, but in this post there is already userAccountName, which I have to get from the people picker only when it loses the focus. – users1100 Nov 01 '13 at 12:14
0

You can get login name using key property,

PickerEntity objEntity = (PickerEntity)peUser.ResolvedEntities; loginName = objEntity.Key;

and Display name using DisplayText property,

displayName = objEntity.DisplayText;

Suren
  • 407
  • 4
  • 23