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.
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.
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
You find the file “spjs-utility.js” here, ensure you grab the version dated 18.09.2010 or newer.
Post any question below.
Alexander
October 13, 2011 at 9:33 pm |
Your link to spjs-utility.js is broken. Where can I get this from?
October 13, 2011 at 11:02 pm
Look OK from here, could you try again?
Alexander
November 8, 2011 at 6:26 pm |
Hi Alexander, I cannot open the spjs.utility.js either, it says the page is not available
November 9, 2011 at 3:47 pm |
Hi all, I’ve found the spjs.utility.js from here http://clevelandhomeless.org/SiteAssets/spjs-Utility.js
thanks Alexander for this helpful code
and it worked
November 9, 2011 at 4:03 pm
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
November 9, 2011 at 4:53 pm |
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
November 9, 2011 at 4:54 pm |
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
November 9, 2011 at 5:03 pm
Hi,
No doubt it’s my file, but it’s located on someone else’s server and they might have changed the file – and also note of this article refers to the file dated 18.09.2010 – a newer version of the file you found.
Try this link where i have changed from https to http:
http://bautz.homeserver.com/SharePointJavascript/Scripts/spjs-utility/
If you still cannot access it I’ll send it to you.
Alexander
November 9, 2011 at 5:11 pm |
Hi Alexander unfortunately I still could not access to this site, I only have my works email address available is that ok
November 9, 2011 at 5:29 pm
I have emailed it to the address used when you posted this message.
Alexander
November 16, 2011 at 12:32 pm |
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?
December 1, 2011 at 12:14 am
Hi,
Sorry for the late reply,
Try changing line 28 from (data.EMail); to (data.Title);
Alexander
December 6, 2011 at 5:40 pm
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
December 12, 2011 at 1:41 pm |
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
December 14, 2011 at 11:47 pm
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
December 15, 2011 at 11:32 am
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
December 30, 2011 at 9:45 am
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
December 28, 2011 at 12:15 am |
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 ??
December 30, 2011 at 9:48 am
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
December 30, 2011 at 9:24 pm
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;
}
January 8, 2012 at 11:08 am
See this updated version
Alexander