People Picker Field

rss

Posted by Andy Weisse - Jun 3 ’15 at 08:25

Hi!
I need the value of an people picker field as text like 'Andy Weiße'.
If I used this:

fd.field('User').control('ready', function() {
  var selectedUsers = fd.field('User').value();
  for (var i = 0; i < selectedUsers.length; i++) {
    alert('Login: ' + selectedUsers[i].Key);
  }
});

the answer is : domain\weisse ..but I need it in text to compare.
So I need the display name, not the login name
.How can I solve this?

Regards Andy

 

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 5 years 8 months 29 days
    #1 by Dmitry Kozlov Jun 3 ’15 at 11:18

    Hi Andy,

    Please, try the following code:

     
    fd.field('User').control('ready', function() {
      var selectedUsers = fd.field('User').value();
      for (var i = 0; i < selectedUsers.length; i++) {
        alert('Login: ' + selectedUsers[i].DisplayText);
      }
    });
    

    Please, make sure that you use the people piker in the Client mode. You can switch the rendering mode in Forms Designer, property name is Render.

  • Andy Weisse
    Member
    Member for: 4 years 17 days
    #2 by Andy Weisse Jun 3 ’15 at 01:59

    Hi Dmitry!
    Thanks for the answer!
    Unfortunately, the same result with Display text. domain\weisse
    For Explanation. I try to compare the current user with a user in a field.

     

    var getWebUserData = function() {
    context = new SP.ClientContext.get_current();
    web = context.get_web();
    currentUser = web.get_currentUser();
    currentUser.retrieve();
    context.load(web);
    context.executeQueryAsync(function(o, e) {
    user = web.get_currentUser();
    currentuser = user.get_title())
    }, function(o, e) {
    alert('request failed ' + e.get_message() + '\n' + e.get_stackTrace());
    });
    }
    ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");

    fd.field('Antragsteller').control('ready', function() {
    var selectedUsers = fd.field('Antragsteller').value();
    for (var i = 0; i < selectedUsers.length; i++) {
    alert('Login: ' + selectedUsers[i].DisplayText);
    fielduser = selectedUsers[i].DisplayText;
    }
    });

    if(currentuser == fielduser)
    {

    .....

    };

     

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 5 years 8 months 29 days
    #3 by Dmitry Kozlov Jun 4 ’15 at 03:48

    [#2]: You'd better compare users by login names. Please, use the following code to get the login name of the current user:

    user.get_loginName()
    
  • Andy Weisse
    Member
    Member for: 4 years 17 days
    #4 by Andy Weisse Jun 4 ’15 at 06:53

    Yes that works!
    Thanks for your help! Its so easy when you know how it works. ;)
    Have a nice day!

  • Andy Weisse
    Member
    Member for: 4 years 17 days
    #5 by Andy Weisse Jun 5 ’15 at 07:38

    Sorry, it is not working properly. :(

    This is my source code:

    var context = null;
    var web = null;
    var currentUser = null;
    var user = null;
    var currentuser;
    var fielduser;
    var fieldgenehmiger1;

    //Check current user
    var getWebUserData = function() {
    context = new SP.ClientContext.get_current();
    web = context.get_web();
    currentUser = web.get_currentUser();
    currentUser.retrieve();
    context.load(web);
    context.executeQueryAsync(function(o, e) {
    user = web.get_currentUser();
    currentuser = user.get_loginName();

    //Check field user
    fd.field('Genehmiger1').control('ready', function() {
    var selectedUsers = fd.field('Genehmiger1').value();
    for (var i = 0; i < selectedUsers.length; i++) {
    fieldgenehmiger1 = selectedUsers[i].Key;
    }
    });

    alert(currentuser + ' compare ' + fieldgenehmiger1);          <----this is the message 'i:0#.w|domain\sptest1 compare domain\sptest1'

    if(currentuser == fieldgenehmiger1){   
    alert('equal');
    }else{
    alert('not equal');                                                                          <---- result 'not equal'
    }                                                         

    }, function(o, e) {
    alert('request failed ' + e.get_message() + '\n' + e.get_stackTrace());
    });
    }
    ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");

    //End source code

     

    I had also nested functions, because the result of the functions sometimes was undefined.
    (In firefox the result of the 'Check field user' function is almost always undefinded.

    I did not think that it is difficult to compare two values.

     


  • RMIC
    Member
    Member for: 4 years 1 month 9 days
    #6 by RMIC Jun 5 ’15 at 07:48

    [#5]: For me, the name appears correctly: First name Last name:

     

    SP.SOD.executeOrDelayUntilScriptLoaded((function() {

    var ctx = new SP.ClientContext.get_current();

    var web = ctx.get_web();

    ctx.load(web);

    var user = web.get_currentUser();

    user.retrieve();

    ctx.executeQueryAsync(

    function() {

    fd.field('AssignedTo').control().value(user.get_loginName());

    fd.field('AssignedTo').control()._el().find('a:eq(0)').click();

    });

    }), "SP.js");

    Simply insert in JavaScript editor.
  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 5 years 8 months 29 days
    #7 by Dmitry Kozlov Jun 5 ’15 at 08:41

    [#5]: Have you tried to use groups functionality to create different forms depending on field values?

    http://spform.com/documentation/groups

    There is a function: ContainsCurrentUser([FieldName])

  • Andy Weisse
    Member
    Member for: 4 years 17 days
    #8 by Andy Weisse Jun 5 ’15 at 09:20

    So it would work wonderfully, yes. So I had made it to show specific users different forms.
    But my boss says that everything has to work in a single Edit form. Otherwise it would be difficult to maintain.
    And therefore I need to compare field user and current user with javascript.

  • Andy Weisse
    Member
    Member for: 4 years 17 days
    #9 by Andy Weisse Jun 5 ’15 at 09:59
    I think I have it. I have changed the field to server rendering mode. SP.SOD.executeOrDelayUntilScriptLoaded((function() {
    var ctx = new SP.ClientContext.get_current();
    var web = ctx.get_web();
    ctx.load(web);
    user = web.get_currentUser();
    user.retrieve();
    ctx.executeQueryAsync(
    function() {
    currentuser = user.get_loginName();

    fieldgenehmiger1 = fd.field('Genehmiger1')
    .value().dictionaryEntries[0].AccountName;
    alert(currentuser + ' compare ' + fieldgenehmiger1);    <--- now the result is 'i:0#.w|domain\sptest1 compare i:0#.w|domain\sptest1' if(user.get_loginName() == fieldgenehmiger1){   
    alert('equal');
    }else{
    alert('not equal');    <--- and the if else function works too!
    }
    });   
    }), "SP.js"); ...I will test again exactly on Monday. Oh man, what a crap! ;) Thank you again Dmitry and RMIC. I wish you a nice weekend!
Displaying 1 to 9 of 9 messages