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?

Popular Posts

SharePoint Interview Questions and Answers

Download Infopath Form Templates

How to get current logged user information using JavaScript ?

Steps to set Form based authentication (FBA) for SharePoint 2010

SharePoint Interview Questions and Answers II

Get List Items - JavaScript

Cross Site List Rollup Web Part for SharePoint 2010

Hide Recently Modified Items

Change Language for current user with JSOM in SharePoint Online

SharePoint 2010 CSS Chart