Pull e-mail from people picker and write to a separate textfield – updated version

08.01.2012 I have published an updated version of this script here


I have previously posted a solution for pulling the e-mail address from a people picker and writing it to a separate field when saving the form. You find it here.

This one does the same, but i have updated the scripts and added an “onchange event” that triggers when you enter the name in the input field (and move focus away from the field), or uses the address book to pick a user.

This solution is designed for single selection people picker fields.

For Internet Explorer in SharePoint 2007, and for most browsers in SharePoint 2010, the email is pulled from the picker itself. For all other browsers than IE in SharePoint 2007, the email is retrieved by using a call to the built in “user list” in SharePoint using the function “getUserInfo_v2”. This function is part of “spjs-utility.js” and can be found below.

How to use

Insert a ContentEditorWebPart (CEWP) under the form in NewForm.aspx / EditForm.aspx by switching the page to edit mode. Read here on how to do it in SharePoint 2007. In SharePoint 2010 you can add the CEWP on the list ribbon menu “Form Web Parts”.

CEWP code for both SharePoint 2007 and 2010:
[javascript]
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript" src="/test/EMSKJS/PeoplePickerEmail/spjs-utility.js"></script>
<script type="text/javascript">

fields = init_fields_v2();
pullEmailFromPickerOnChange({from:’MyPeoplePicker’,to:’Title’});

/******************************************************
Do not change anything below this line
*******************************************************/
function pullEmailFromPickerOnChange(obj){
var toFind, data;
$(document).ready(function(){
toFind = "div.ms-inputuserfield";
if(!browseris.ie5up && typeof(_fV4UI)===’undefined’){
toFind = "textarea";
}
$(fields[obj.from]).find(toFind).bind(‘blur’,function(){
data = pullEmailFromPicker(obj.from,obj.to);
if(data.EMail!==undefined){
$(fields[obj.to]).find(‘input’).val(data.EMail);
}
});
$(fields[obj.from]).find(‘img:last’).bind(‘click’,function(){
setTimeout(function(){
data = pullEmailFromPicker(obj.from,obj.to);
if(data.EMail!==undefined){
$(fields[obj.to]).find(‘input’).val(data.EMail);
}
},500);
});
});
}

function pullEmailFromPicker(finFrom,finTo){
var result, isResolved, data, matchArrRaw, matchArr;
result = {};
$(fields[finFrom]).find(‘.ms-formbody’).find("div[id=’divEntityData’]").each(function(){
isResolved = ($(this).attr(‘isresolved’).toLowerCase()==’true’)?true:false;
if(isResolved){
data = $(this).find(‘div:first’).attr(‘data’);
matchArr = [];
matchArrRaw = data.split(/<[^>]*>/);
$.each(matchArrRaw,function(i,val){
if(val!==”){
matchArr.push(val);
}
});
if(matchArr.length>1){
$.each(matchArr,function(i,val){
if(i%2===0){
switch(val){
case’SPUserID’:
val = ‘ID’;
break;
case ‘Email’:
val = ‘EMail’;
break;
case ‘DisplayName’:
val = ‘Title’;
break;
}
result[val] = matchArr[i+1];
}
});
}else{ // Non IE in SP2007
result = getUserInfo_v2($(this).attr(‘description’));
}
}
});
return result;
}
</script>
[/javascript]

Note to SharePoint 2010 users:
Add this code to a text file and put it in a document library, then use the content link option on the CEWP to link to this code. This is necessary to overcome a possible “bug” in the CEWP handling when editing a SP2010 page where the HTML is accumulated when editing the page.

This tip is also recommended for SharePoint 2007 users, but is not absolutely necessary.

Parameters

You call this function using an object literation like this
pullEmailFromPickerOnChange({from:’MyPeoplePicker’,to:’Title’});

The attribute “from” is the FieldInternalName of the people picker and “to” is the FieldInternalName of the field to write the email to.
Read here on how to find the FieldInternalName

Download

You find the file “spjs-utility.js” here, ensure you grab the version dated 18.09.2010 or newer.


Post any question below.

Alexander

30 thoughts on “Pull e-mail from people picker and write to a separate textfield – updated version”

    1. Hi,
      This page is NOT hosted by me and i cannot guarantee this file has not been modified. Also, it is not the latest version – It’s from 2010
      .
      I have double checked and the server is up.

      Are you still having trouble?

      Alexander

  1. Alexandar, I still have troubled opening the link that you have provided the error is Internet Explorer cannot display the webpage

    Most likely causes:
    You are not connected to the Internet.
    The website is encountering problems.
    There might be a typing error in the address.

    What you can try:
    Diagnose Connection Problems

    More information

    This problem can be caused by a variety of issues, including:

    Internet connectivity has been lost.
    The website is temporarily unavailable.
    The Domain Name Server (DNS) is not reachable.
    The Domain Name Server (DNS) does not have a listing for the website’s domain.
    If this is an HTTPS (secure) address, click Tools, click Internet Options, click Advanced, and check to be sure the SSL and TLS protocols are enabled under the security section.

    For offline users

    You can still view subscribed feeds and some recently viewed webpages.
    To view subscribed feeds

    Click the Favorites Center button , click Feeds, and then click the feed you want to view.

    To view recently visited webpages (might not work on all pages)

    Click Tools , and then click Work Offline.
    Click the Favorites Center button , click History, and then click the page you want to view.

    Can you post another link

      1. Excellant!!! Thanks Alexander, I’ve tried it and it worked perfectly, this will make form filling so much easier!!! thanks again, have a fabulous xmas 🙂

  2. Hi Alexander, me again sorry to bother you again :(, I have managed to get ‘Title, Department and Email’ but I stumped getting first name and last name, I’ve tried all different ways of adding this and also adding _x0020_ between the two words but it still will not bring it up, I am looking to find first and last name and also the work phone number. Can you help me define how to draw this info 🙂

    1. Hi,
      Pulling this information from the people picker cannot be done directly. You would have to pull the login name from the picker, and then use “getUserInfo_v2()” to get the profile from the “user list” in SharePoint.

      If this information is available there, you can reach it.

      Alexander

      1. Thanks Alexander I really appreciate you patience with me on this, I will try the method you have suggested, just a small query, what would the internal name be for the login name? In the people picker information box it is called ‘Account’ but if I add that in the data.Account, nothing appears, thanks again for your patience, this is really helping me alot 🙂

    2. Hi,
      Sorry for the late reply.

      When using “getUserInfo_v2()” the “Account” FieldInternalName is “Name”, and the “Name” FieldInternalName is “Title”.

      A bit confusing, but if you view the page source and search for “Account”, you’ll find it.

      Alexnader

  3. Hi Alexander, I ave to admit that you are dooing great job :). Your scripts are very helpfull. I’ve got question regarding above comment. I have similiar situation, I’d like to get cellnumber of selected people. I have field called CellPhone (this is field internal name on ‘user list’), I’ve added ‘CellPhone’ field in arrOfFields in function getUserInfo_v2, I’ve also changed data.EMail to data.CellPhone in pullEMailFromPickerOnChange function but nothing happens after choosing people form picker. Your funciton with pulling email works like harm but my doesn’t. Could you help me with my problem ??

    1. Hi,
      You’ll have to rewrite the “pullEMailFromPickerOnChange” to always use “getUserInfo_v2()”. As it is setup now, it uses “getUserInfo_v2()” only for non-IE browsers under SharePoint 2007.

      Alexander

      1. Hi Alexander, I’ve tried modify pullEmailFromPickerOnChange as you wrote but still no effect. I would be appreciate if you can look into code after my changes, what I’ve done wrong:

        fields = init_fields();
        pullMobileFromPickerOnChange({from:’AS400Requestor’,to:’AS400RequestorMobile’});

        /******************************************************
        Do not change anything below this line
        *******************************************************/
        function pullMobileFromPickerOnChange(obj){
        var toFind, data;
        $(document).ready(function(){
        toFind = “div.ms-inputuserfield”;
        if(!browseris.ie5up && typeof(_fV4UI)===’undefined’){
        toFind = “textarea”;
        }
        $(fields[obj.from]).find(toFind).bind(‘blur’,function(){
        data = pullMobileFromPicker(obj.from,obj.to);
        if(data.CellPhone!==undefined){
        $(fields[obj.to]).find(‘input’).val(data.CellPhone);
        }
        });
        $(fields[obj.from]).find(‘img:last’).bind(‘click’,function(){
        setTimeout(function(){
        data = pullMobileFromPicker(obj.from,obj.to);
        if(data.CellPhone!==undefined){
        $(fields[obj.to]).find(‘input’).val(data.CellPhone);
        }
        },500);
        });
        });
        }

        function pullMobileFromPicker(finFrom,finTo){
        var result, isResolved, data, matchArrRaw, matchArr;
        result = {};
        $(fields[finFrom]).find(‘.ms-formbody’).find(“div[id=’divEntityData’]”).each(function(){
        isResolved = ($(this).attr(‘isresolved’).toLowerCase()==’true’)?true:false;
        if(isResolved){
        data = $(this).find(‘div:first’).attr(‘data’);
        matchArr = [];
        matchArrRaw = data.split(/]*>/);
        $.each(matchArrRaw,function(i,val){
        if(val!==”){
        matchArr.push(val);
        }
        });
        if(matchArr.length>1){
        $.each(matchArr,function(i,val){
        if(i%2===0){
        switch(val){
        case’SPUserID’:
        val = ‘ID’;
        break;
        case ‘CellPhone’:
        val = ‘CellPhone’;
        break;
        case ‘DisplayName’:
        val = ‘Title’;
        break;
        }
        // result[val] = matchArr[i+1];
        result = getUserInfo_v2($(this).attr(‘description’));
        }
        });
        }else{ // Non IE in SP2007
        result = getUserInfo_v2($(this).attr(‘description’));
        }
        }
        });
        return result;
        }

  4. I can’t figure out how to get this to work. I have a people picker field and I want to write the e-mail of the selected person to a multi-line text field. Here is my code:

    fields = init_fields_v2();
    var prinInvest = $(fields[‘principalInvestigatorID’]);
    var prinInvestContact = $(fields[‘principalInvestigatorContactID’]);
    pullEmailFromPickerOnChange({from:prinInvest,to:prinInvestContact});

    /******************************************************
    Do not change anything below this line
    *******************************************************/
    function pullEmailFromPickerOnChange(obj){
    var toFind, data;
    $(document).ready(function(){
    toFind = “div.ms-inputuserfield”;
    if(!browseris.ie5up && typeof(_fV4UI)===’undefined’){
    toFind = “textarea”;
    }
    $(fields[obj.from]).find(toFind).bind(‘blur’,function(){
    data = pullEmailFromPicker(obj.from,obj.to);
    if(data.EMail!==undefined){
    $(fields[obj.to]).find(‘input’).val(data.EMail);
    }
    });
    $(fields[obj.from]).find(‘img:last’).bind(‘click’,function(){
    setTimeout(function(){
    data = pullEmailFromPicker(obj.from,obj.to);
    if(data.EMail!==undefined){
    $(fields[obj.to]).find(‘input’).val(data.EMail);
    }
    },500);
    });
    });
    }

    function pullEmailFromPicker(finFrom,finTo){
    var result, isResolved, data, matchArrRaw, matchArr;
    result = {};
    $(fields[finFrom]).find(‘.ms-formbody’).find(“div[id=’divEntityData’]”).each(function(){
    isResolved = ($(this).attr(‘isresolved’).toLowerCase()==’true’)?true:false;
    if(isResolved){
    data = $(this).find(‘div:first’).attr(‘data’);
    matchArr = [];
    matchArrRaw = data.split(/]*>/);
    $.each(matchArrRaw,function(i,val){
    if(val!==”){
    matchArr.push(val);
    }
    });
    if(matchArr.length>1){
    $.each(matchArr,function(i,val){
    if(i%2===0){
    switch(val){
    case’SPUserID’:
    val = ‘ID’;
    break;
    case ‘Email’:
    val = ‘EMail’;
    break;
    case ‘DisplayName’:
    val = ‘Title’;
    break;
    }
    result[val] = matchArr[i+1];
    }
    });
    }else{ // Non IE in SP2007
    result = getUserInfo_v2($(this).attr(‘description’));
    }
    }
    });
    return result;
    }

    I am using a customized form so I had to use the special init_fields_v2() function which I replaced the original in the spjs-utility.js file. Please help.

      1. I did follow those instructions, as you can see above in my code when I used the other init_fields_v2() function and the reference $(fields[‘principalInvestigatorID’]);. It still does not work

    1. Hi,
      When using a customized form things will be a bit more uncertain. Have you tested the code in a unmodified form to ensure you get it working?

      If you are using jQuery above 1.7x you might need to test with 1.6.4 as this script might not work with 1.7x (I have not tested it).

      Alexander

  5. Hi Alex, I need to pull from text field and copy it into two different columns. Example: column1 = SHPS011SSAD copy into column2 = SH and column3 = PS. Is there a javascript solution for this?

  6. Hi Alexander,
    Thank you very much for this post.
    I want to pull user infomation as Domainname\userId(ex.DuPont\Puskar_rich01).Kindly suggest which property should I use instead od data.EMail?

    Thanks

    1. Hi,
      Use this code:

      	
      var loginArr = [];
      var login = $(fields["yourFieldInternalNameHere"]).find('.ms-formbody').find("div[id='divEntityData']").each(function(i,d){
      	loginArr.push($(d).attr("key"));
      });
      alert(loginArr);

      Alexander

  7. Hi Alexander,

    I just found this wonderful solution of yours but I cant get it to work. Im using the below code on my NewForm. I have tried it both on the custom and the default New item form. Any idea? Thanks in advanced. – Arnel

    fields = init_fields_v2();
    pullEmailFromPickerOnChange({PP:’MyPeoplePicker’,email:’Title’});

    /******************************************************
    Do not change anything below this line
    *******************************************************/
    function pullEmailFromPickerOnChange(obj){
    var toFind, data;
    $(document).ready(function(){
    toFind = “div.ms-inputuserfield”;
    if(!browseris.ie5up && typeof(_fV4UI)===’undefined’){
    toFind = “textarea”;
    }
    $(fields[obj.from]).find(toFind).bind(‘blur’,function(){
    data = pullEmailFromPicker(obj.from,obj.to);
    if(data.EMail!==undefined){
    $(fields[obj.to]).find(‘input’).val(data.EMail);
    }
    });
    $(fields[obj.from]).find(‘img:last’).bind(‘click’,function(){
    setTimeout(function(){
    data = pullEmailFromPicker(obj.from,obj.to);
    if(data.EMail!==undefined){
    $(fields[obj.to]).find(‘input’).val(data.EMail);
    }
    },500);
    });
    });
    }

    function pullEmailFromPicker(finFrom,finTo){
    var result, isResolved, data, matchArrRaw, matchArr;
    result = {};
    $(fields[finFrom]).find(‘.ms-formbody’).find(“div[id=’divEntityData’]”).each(function(){
    isResolved = ($(this).attr(‘isresolved’).toLowerCase()==’true’)?true:false;
    if(isResolved){
    data = $(this).find(‘div:first’).attr(‘data’);
    matchArr = [];
    matchArrRaw = data.split(/]*>/);
    $.each(matchArrRaw,function(i,val){
    if(val!==”){
    matchArr.push(val);
    }
    });
    if(matchArr.length>1){
    $.each(matchArr,function(i,val){
    if(i%2===0){
    switch(val){
    case’SPUserID’:
    val = ‘ID’;
    break;
    case ‘Email’:
    val = ‘EMail’;
    break;
    case ‘DisplayName’:
    val = ‘Title’;
    break;
    }
    result[val] = matchArr[i+1];
    }
    });
    }else{ // Non IE in SP2007
    result = getUserInfo_v2($(this).attr(‘description’));
    }
    }
    });
    return result;
    }

Leave a Reply