Display Default Value In People Picker Control in List NewForm

In SharePoint 2010 OnPremise, our customer came up with one new small requirement to show the default value for the people picker control in Custom List OOB New Form. As it was for a simple OOB new form for a Custom List, we analyzed the options accordingly in 2010.

  • If you want to do it in InfoPath Form then there is very nice article presented here by Laura Rogers.
  • If you want to do it in SharePoint 2013 then please refer this nice article which describes how to do it with JSLink.

In our case it was SharePoint 2010 and a Custom List OOB New Form. Actually the problem is we cannot set default values for People Picker control from design time so we had two options basically, one with the help of Custom JQuery Injection or second with the help of third party SharePoint Add-On.

We observed the following Pros and Cons for the SharePoint Add-On:

Pros:

    - It can be very useful for setting default values during design time for lookup or people picker fields.

Cons:

    - It is not free. Free trial period is only for 30 days.
    - Setup will install a new wsp to the farm so might be a problem with maintenance or updates in future.

So we decided to go ahead with the first option instead i.e. Custom JQuery Injection approach, that was more simple and quick to implement.

For this we edited the default new form for the custom list, then added one Content editor webpart in the form so as to attach a custom .js file with it (Remember there was no JSLink in SP 2010). Now is the time to create actual JQuery file which will inject default value into the People Picker control.

In our case Customer wanted to show a particular person’s name in the people picker and not the logged in person as default. If you have the requirement to show the logged in user as default then you may refer this link with some little variation and use of “jquery.SPServices” libraries.

We created following JQuery Injection script for our purpose. Here is the custom JQuery script named “setdefaultvalue.js” (I hope you have “jquery.min.js” already available in “_layouts/js” folder in 14 hive, if not you can download it from here and place it in 14 hive path).

setdefaultvalue.js

  1. <script src="/_layouts/js/jquery.min.js"></script>  
  2. <script type="text/javascript">  
  3. $(document).ready(function ()  
  4. {  
  5.     user = "Domain\\Username"// Set “Domain\\Username”as default user  
  6.     // Set “user” as default value in the required people picker control  
  7.     var counter = 0;  
  8.     $('div[title="People Picker"]').each(function ()  
  9.     {  
  10.         if (counter == 1)  
  11.         {  
  12.             $(this).html(user);  
  13.         }  
  14.         counter++;  
  15.     });  
  16.     // Call the click event of “Check names” button explicitly  
  17.     counter = 0;  
  18.     $('a[title="Check Names"]').each(function ()  
  19.     {  
  20.         if (counter == 1)  
  21.         {  
  22.             $(this).click();  
  23.         }  
  24.         counter++;  
  25.     });  
  26. });  
  27. </script>  
Note: In this script we use the “counter” variable to check at which position our People Picker control appears in the form (required only in case of multiple People Picker control on form).

And then uploaded this file in that site collection’s Style Library,

Style Library

And then referred to it in the Content Editor webpart property “Content Link” as below

Content Link

Just make sure of one thing, keep the Zone index of the content editor webpart later than the main form webpart. Suppose if the main form webpart zone index is 1,  then keep the zone index of content editor webpart as two. This will make sure that all the controls in the form are loaded first and then JQuery script will fire.

index

After all these steps, the default value started showing up correctly in the desired People Picker control on the list new form.

Read more articles on SharePoint:

Up Next
    Ebook Download
    View all
    Learn
    View all