vbCity
Migrating to the new kind of Visual Basic Developer Community

How to set SharePoint People Picker default value to current user through javascript

I found this great POST by the SharePoint Designer team for “Using Javascript to Manipulate a List Form Field”.  Unfortunately it didn't work very well for People Picker fields which is exactly what I needed to do today.

After some trial and error, and a bit of head scratching I finally came up with the follow which I thought I'd share, since when I was searching for a solution I found a lot of people in a similar situation with no solution posted.

=0 && i
h)ch.style.height=h;" selcount="3" selold="1" sel="1">Code CopyHideScrollFull
<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("fillDefaultValues");

function
fillDefaultValues()
{
fillPeoplePickerWithCurrentUser('Submitted_x0020_By');
}

function
fillPeoplePickerWithCurrentUser(pickerName)
{
//get the current user from the welcome menu
var
currentUser = getCurrentUser();

//check to see that we've got it

if
(currentUser != null)
{
//get the people pickers input div
var
pp = getPickerInputElement(pickerName);
//set it to the current user if we've found it

if
(pp != null)
pp.innerHTML = currentUser;
}
}

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

function
getPickerInputElement(fieldsInternalName)
{
var result  = "";
var
divs = document.getElementsByTagName("DIV");
for
(var i=0; i < divs.length ; i++)
{
if(divs.id=="WebPartWPQ2")
{
var tds = divs.getElementsByTagName("TD");
for
(var j=0; j < tds.length; j++)
{
var cellHTML = tds[j].innerHTML;
if(cellHTML.indexOf('FieldInternalName="' + fieldsInternalName + '"') >= 0)
{
var innerDivs = tds[j].getElementsByTagName("DIV");
for
(var k=0; k < innerDivs .length; k++)
{
if(innerDivs[k].id.indexOf("UserField_upLevelDiv") > 0)
{
result = innerDivs[k];
break
;
}
}
}
}
}
}
return
result;
}
</
script>
. . .

Posted Apr 10 2008, 09:08 AM by Admin

Comments

Colin wrote re: How to set SharePoint People Picker default value to current user through javascript
on 06-12-2008 5:37 PM

Andy, great code-snippet on _spBodyOnLoadFunctionNames

<br>

<br>thanks heaps.  I will use it right through our company intranet.

<br>

<br>Cheers,

<br>Colin.

Jeremy Sinclair wrote re: How to set SharePoint People Picker default value to current user through javascript
on 06-19-2008 5:21 AM

I've posted a more efficient way of retrieving this value on my blog

<br>

<br><a target="_new" href="www.asinote.com/.../a>

Mirrored Blogs wrote SharePoint Kaffeetasse #82
on 07-03-2008 12:41 AM

Anpassung SharePoint/MOSS Color Picker Custom Field How to set SharePoint People Picker default value

w wrote re: How to set SharePoint People Picker default value to current user through javascript
on 09-11-2008 2:10 AM

fdbd

Ashish wrote re: How to set SharePoint People Picker default value to current user through javascript
on 11-15-2008 4:21 AM

As per your script you set for logged in user, I am looking for some ting in different manner, I want to assign another user in this field, please let me know is this posible

Suzuki Fairings wrote re: How to set SharePoint People Picker default value to current user through javascript
on 12-16-2008 5:45 AM

Very good job!

evilgenius wrote re: How to set SharePoint People Picker default value to current user through javascript
on 12-31-2008 10:14 PM

Jeremy's solution works great if you keep the default master page data. If you remove it, well, the posted solution still works too!

allan wrote re: How to set SharePoint People Picker default value to current user through javascript
on 01-03-2009 5:58 AM

it is great, i am searching a lot and finally find your blog out,

<br>because i am a newer, i have copied the code and put it in my right place, but i do not know what item i need to replace. need your help, thank you in advanced.

Copyright 1998-2009 vbCity.com LLC