Set default value for People Picker via JavaScript

First open entry form (NewForm.aspx / EditForm.aspx / DispForm.aspx) in advanced mode in sharepoint designer. Then search for tag :


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


Now copy & paste this JavaScript code after this tag :


<script type="text/ecmascript">
 //Function search for people picker by its no and set default value string type
 //If we have 2 people picker in entry form & we want to set value to 2nd no. then "pickerNo=2"
 function fillPeoplePickerWithCurrentUser(pickerNo,userLoginName )
   {
      if(userLoginName != null)
      {
         var pp = getPickerImputElement(pickerNo);
  
         if(pp != null)
         {
           pp.innerHTML = userLoginName;
         }
      }
   }

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

Comments

Anonymous said…
Hey gaurav,

Good collection of intrviews question,it will help those people who are looking for a job change in sharepoint administrator..Keep it up..
Anonymous said…
Your code saved my sorry god damn poor ass. Thanks dude.
Anonymous said…
This is not working for me. Will this script work with no modification on a NewForm.aspx or do I need to set some of the values?