Pull login name from People Picker Field

Forums SPJS-Utility Pull login name from People Picker Field

Tagged: 

This topic contains 5 replies, has 2 voices, and was last updated by  Alexander Bautz 1 month ago.

  • Author
    Posts
  • #21904

    Andrew Fletcher
    Participant

    Hi –
    I was using SPJS-Utility as shown in this post to pull a few fields from a list item that were then used to prepopulate corresponding fields in a new item form. With a recent upgrade of our from SP2010 to SP2013 the prepopulated person fields are no longer working (see attachment). It looks like SP2013 wants either login names or email addresses instead of a lastname, firstname format. Is there a way SPJS Utility can pull either of those formats from a people picker field?
    Thanks,
    Andrew

    Attachments:
  • #21910

    Alexander Bautz
    Keymaster

    Hi,
    Yes, use this format:

    var loginName = spjs.utility.getFieldValue({"fin":"YourPeoplePicker","key":"loginName"});

    This will return an array like this – with only one item if the PP is single choice:

    ["i:0#.f|membership|username@domain.com"]

    Alexander

  • #21916

    Andrew Fletcher
    Participant

    Thanks Alexander! I apologize. I probably didn’t explain enough of what I was doing. I have a URL in a column of List1 that points to a List2 new item form with the ID of the List1 item tacked on the end. On the List2 new form I have DFFS installed with a rule that runs the spjs.utility.queryItems to get certain fields from the item in List1 and drops them into the corresponding fields in the new item form. Am I limited to the firstname, lastname format like that? Relevant code in the List2 new item form is below. Thanks so much for you help!

    
    
    function clone(){
     var cloneid = getParameterByName('cloneid');
        var res = spjs.utility.queryItems({
            "listName": "List1",
            "query": "<Where><Eq><FieldRef Name='ID'/><Value Type='Number'>" + cloneid + "</Value></Eq></Where>",
            "viewFields": ["Title", "PeoplePicker1", "PeoplePicker2", "PCMID"]
        });
     if (res.count > 0) {
      jQuery.each(res.items, function (index, item) {
       setFieldValue("Title",item.Title);
       item.PeoplePicker1 = item.PeoplePicker1.substring(item.PeoplePicker1.indexOf("#")+1);
       setFieldValue("PeoplePicker1",item.PeoplePicker1);
       item.PeoplePicker2 = item.PeoplePicker2.substring(item.PeoplePicker2.indexOf("#")+1);
       setFieldValue("PeoplePicker2",item.PeoplePicker2);
       setFieldValue("PCMID",item.PCMID);
      }
     )}
    }
  • #21923

    Alexander Bautz
    Keymaster

    In this case you only get the userId and the display name back from the query, and must make another call to get the login name. Do it like this:

    if(item.PeoplePicker1 !== null){
        setFieldValue("PeoplePicker1",spjs.utility.userInfo(item.PeoplePicker1.split(";#")[0]).Name);
    }

    Alexander

  • #22537

    Andrew Fletcher
    Participant

    Alexander —

    Thanks for the reply. I’d been moved to a different project for a few months so I didn’t have a chance to test this until today. It worked perfectly!

    Thanks again,
    Andrew

  • #22539

    Alexander Bautz
    Keymaster

    Thanks for the feedback – I’m glad it worked.

    Alexander

You must be logged in to reply to this topic.