Hi friends, In this tutorial, we are going to learn how to fetch the current username and set its value to any SharePoint field. To get the 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?
- Open your list new item page.
- Click on Settings button (Gear icon) and click on Edit page.
- Add Content Editor Web Part.
- Copy the following code and paste it in above Web Part.
- Click on Stop Editing and it’s done!
In this example, we are getting Name of Current User and setting its value to the Full Name field.
Now whenever you open the page for adding a new item in the SharePoint list, you can see the name of the current user is auto-populated in the Full Name field. See the screenshot below.
If you like this article, please share it with your friends and like our 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!