in

vbCity Blogs

New (temp) place for vbCity Blogs

Andy Bonner's Blog

My SharePoint titbits and other things that take my fancy.

Set SharePoint PeoplePicker Field mark 2

Here's  an updated version of my PeoplePicker filling script that now uses the position of the  people picker rather than the field name to allow you to fill multiple people pickers easily.

Note : If you tried my code & couldn't get it to work, it's because square brackets weren't displaying on this site so I've had to put a space after the integer when getting the value from the array to get it to show here.

 

<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("fillDefaultValues");


function fillDefaultValues()
{
    //fill the  first people picture
 fillPeoplePickerWithCurrentUser(1);
 //fill th esecond people picker
 fillPeoplePickerWithCurrentUser(2);
}


function fillPeoplePickerWithCurrentUser(pickerNo)
{
 var currentUser = getCurrentUser();


 if(currentUser != null)
 {
  var pp = getPickerImputElement(pickerNo);
  
  if(pp != null)
  {
   pp.innerHTML = currentUser;
  }
 }
}



function getPickerImputElement(pickerNo)
{
 var result = '';
 var divs = document.getElementsByTagName('DIV');
 var j = 0;


 for(var i=0; i < divs.length; i++)
 {
  if(divs[i ].id.indexOf('UserField_upLevelDiv') > 0)
  {
   j++;
   
   if(j == pickerNo)
   {
    result = divs[i ];
    break;
   }
  }
 }
 return result;
}


function getCurrentUser()
{
 var tags = document.getElementsByTagName('a');
 for(var i=0; i < tags.length; i++)
 {
  if(tags[i ].innerText.substr(0,7) == 'Welcome')
   return tags[i ].innerText.substr(8,tags[i ].innerText.length);
 }
}
</script>


 

Only published comments... Nov 04 2008, 04:43 AM by Andy Bonner
Filed under: ,

Comments

 

dishaniti said:

Hello

I am new bie in programming, can you please let me know the exact position where I need to add your javascript?

I want to add on new and edit record page.

Disha

December 3, 2008 6:29 AM
 

Andy Bonner said:

Hi Disha

I place it immediately after this tag

<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">

regards

Andy

December 3, 2008 9:30 AM
 

grinsepilz said:

Hi Andy,

This script doesn't work for me.

I also tried to solve it by myself, but whereever I fill in the Username (the Div, the hidden Inputfields or the Textarea) by Javascript it never showed and saved this Username in the Form -.-

Any help or suggestions?

March 2, 2009 5:33 AM
 

Andy Bonner said:

Hi Grinsepilz

Have a look at the code now, you weren't seeing all the code before as anything placed between square brackets wasn't showing here so  I've had to put a space after the integer value to get it to display.

Try it now & let me know how you get on.

March 2, 2009 10:00 AM
 

grinsepilz said:

Thanks,

now your script is working, too, sorry solved it by myself, but I hope others will from now on benefit from this :)

Bye

March 6, 2009 7:57 AM

About Andy Bonner

 

Copyright 1998-2009 vbCity.com LLC
Powered by Community Server (Non-Commercial Edition), by Telligent Systems