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:

<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>

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

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

  1. Larry Bradshaw Says:

    Your link to spjs-utility.js is broken. Where can I get this from?

  2. Kris Says:

    Hi Alexander, I cannot open the spjs.utility.js either, it says the page is not available

  3. Kris Says:

    Hi all, I’ve found the spjs.utility.js from here http://clevelandhomeless.org/SiteAssets/spjs-Utility.js
    and it worked :-) thanks Alexander for this helpful code

    • Alexander Bautz Says:

      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

  4. Kris Says:

    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

  5. Kris Says:

    also in the spjs.utility.js I downloaded it has the following entry when I open in note book
    /*********************************
    * Created by Alexander Bautz
    * alexander.bautz@gmail.com
    * http://sharepointjavascript.wordpress.com
    * Copyright (c) 2009-2010 Alexander Bautz (Licensed under the MIT X11 License)
    * LastMod: 14.06.2010
    **********************************/

    so it must originate from yourself

  6. Kris Says:

    Hi Alexander unfortunately I still could not access to this site, I only have my works email address available is that ok

  7. Kris Says:

    Hi Alexander, thanks again for this, just a small question, is there anyway to get the user first name, last name etc. using this function?

    • Alexander Bautz Says:

      Hi,
      Sorry for the late reply,
      Try changing line 28 from (data.EMail); to (data.Title);

      Alexander

    • Kris Says:

      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 :-)

  8. Kris Says:

    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 :)

    • Alexander Bautz Says:

      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

    • Kris Says:

      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 :)

    • Alexander Bautz Says:

      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

  9. Pawel Says:

    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 ??

    • Alexander Bautz Says:

      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

    • Pawel Says:

      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;
      }

    • Alexander Bautz Says:

      See this updated version

      Alexander

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 435 other followers