Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

Set SharePoint People Picker Default Value to Current User

$
0
0
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, 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 Userset sharepoint people picker default value current user

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

Viewing all articles
Browse latest Browse all 1058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>