SPServices Get Current User Name & Set to SharePoint Field Name

Hi friends, In this tutorial we are going to learn how to fetch the current user name and set its value to any SharePoint field. For getting current SharePoint user information we can use SPServices.

There is one utility function of SPServices called SPGetCurrentUser which has the functionality to return information about the current user.

How  $().SPServices.SPGetCurrentUser works?

The SPGetCurrentUser function does an AJAX call to grab /_layouts/userdisp.aspx?Force=True and “scrapes” the values from the page based on the internal field name (aka StaticName).

How to get Current User Name and Set it to SharePoint field?

  1. Open your list new item page.
  2. Click on Settings button (Gear icon) and click on Edit page.
  3. Add Content Editor Web Part.
  4. Copy the following code and paste it in above Web Part.
  5. Click on Stop Editing and its done!

In this example we are getting Name of Current User and setting its value to Full Name field.

<div> 
 <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> 
 <script src="http://www.myclassbook.org/wp-content/uploads/2016/07/jquery.SPServices-2014.02.min_.js"></script> 
 <div> 
 <script language="javascript" type="text/javascript"> 
 $(document).ready(function() { 
 // Set the "Full Name" input field to the current user's "Title" property 
 var userName = $().SPServices.SPGetCurrentUser({ 
 fieldName: "Title" 
 }); 
 $("input[Title='Full Name']").val(userName); 
 }); 
 </script> 
 </div> 
</div>

Now whenever you open the page for adding new item in the SharePoint list, you can see the name of current user is auto populated in Full Name field. See screen shot below.

Get Current user name in SharePOint
Get Current user name in SharePoint

If you like this article, please share it with your friends and like or facebook page for future updates. Subscribe to our newsletter to get notifications about our updates via email. If you have any queries, feel free to ask in the comments section below. Have a nice day!

Leave a Reply