Friday, July 5, 2013

Set SharePoint People Picker Default Value to Current User

Requirement: In a request tracking system, wanted to auto populate people picker value from currently logged in User.

Solution:  How to set the People Picker field value to current user? Lets use jQuery and SPServices to fetch the logged in user name and fill the SharePoint list form's people picker field value.

In short, Call SPServices function: SPGetCurrentUser() to retrieve the get the current user. Populate the people picker In SharePoint List Form using jQuery! Place this script in a text file, upload to any SharePoint library, Edit the NewForm.aspx page by appending ?toolpaneview=2 to the URL, Add a CEWP and locate the script in Content editor web part's property.

<!-- jQuery Reference. You can refer it from Layouts Folder/Doc Library too, after uploading the script. -->
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
 
<!-- Download SPServices from: http://spservices.codeplex.com/ Or use this CDN  -->
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.1a/jquery.SPServices-0.7.1a.min.js"></script>

<script type="text/javascript">
 $(document).ready(function() {
     //Get the current user
     var user= $().SPServices.SPGetCurrentUser();
 
      //Set all sharepoint 2010 people picker default to current user
      $("div[title='People Picker']").text(user);
  });
</script>

When we have multiple people picker fields in the SharePoint form, we got to find the one needs to be set.

<!-- jQuery Reference. You can refer it from Layouts Folder/Doc Library too, after uploading the script. -->
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
 
<!-- Download SPServices from: http://spservices.codeplex.com/  -->
<script type="text/javascript" src="http://sharepoint.crescent.com/helpdesk/support/jquery.SPServices-2013.01.min.js"></script>

<script type="text/javascript">
 $(document).ready(function() {
  
  //Get the current user name 
  var user= $().SPServices.SPGetCurrentUser();
 
   //Find the Specific People picker field "Requester" and set its value 
   $().SPServices.SPFindPeoplePicker({
   peoplePickerDisplayName: "Requester",
   valueToSet: user,
   checkNames: true
  });

  }); 

</script> 
Result in action: Set SharePoint People Picker Default Value to Current User
set sharepoint people picker default value current user

Its also possible to set the people picker from current user Using client side object model



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

3 comments :

  1. Its really g8 article...
    helps a lot...

    ReplyDelete
  2. An idea for SPS 2013 ?

    ReplyDelete
  3. How to do the same in sharepoint 2013 people picker?
    thanks in advance

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...