Skullcrusher's Blog

Andy Bonner's Blog at vbCity

vbCity Blogs moved to:
http://cs.vbcity.com/blogs
  Home :: Syndication  :: Login

Congratulations! You've managed to stumble across the mad rantings of Andy Bonner aka Skullcrusher

Here you'll find all sorts of hopefully useful info on MOSS (Microsoft Office SharePoint Server) and things .NET related. You might even find other useful gems if I get around to it.


You can contact me on the address below, but I won't guarantee a response 8-}

JunJuly 2009Aug
SMTWTFS
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

Articles

Archives

Topics

Image Galleries

MOSS/SharePoint Links

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.

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[i].innerText.substr(0,7) == 'Welcome')
{
return tags[i].innerText.substr(8,tags[i].innerText.length);
}
}
}

function
getPickerInputElement(fieldsInternalName)
{
var result  = "";
var
divs = document.getElementsByTagName("DIV");
for
(var i=0; i < divs.length ; i++)
{
if(divs[i].id=="WebPartWPQ2")
{
var tds = divs[i].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 on Thursday, April 10, 2008 9:08 AM

Feedback

# re: How to set SharePoint People Picker default value to current user through javascript 6/12/2008 5:37 PM Colin
Andy, great code-snippet on _spBodyOnLoadFunctionNames

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

Cheers,
Colin.

# re: How to set SharePoint People Picker default value to current user through javascript 6/19/2008 5:21 AM Jeremy Sinclair
I've posted a more efficient way of retrieving this value on my blog

http://www.asinote.com/2008/06/18/RetrievingCurrentUserInSharePointUsingJavascript.aspx

# SharePoint Kaffeetasse #82 7/3/2008 12:41 AM Mirrored Blogs
Anpassung SharePoint/MOSS Color Picker Custom Field How to set SharePoint People Picker default value

# re: How to set SharePoint People Picker default value to current user through javascript 9/11/2008 2:10 AM w
fdbd

# re: How to set SharePoint People Picker default value to current user through javascript 11/15/2008 4:21 AM Ashish
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

# re: How to set SharePoint People Picker default value to current user through javascript 12/16/2008 5:45 AM Suzuki Fairings
Very good job!

# re: How to set SharePoint People Picker default value to current user through javascript 12/31/2008 10:14 PM evilgenius
Jeremy's solution works great if you keep the default master page data. If you remove it, well, the posted solution still works too!

# re: How to set SharePoint People Picker default value to current user through javascript 1/3/2009 5:58 AM allan
it is great, i am searching a lot and finally find your blog out,
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.

Post Feedback

Title:
Name:
Url:
Comments: 
Protected by Clearscreen.SharpHIPEnter the code you see: