Quick Summary

Learn how to pre-populate user details in customised SharePoint NewForm.aspx pages using JavaScript and SharePoint Client Object Model.

Pre-populating a Current User's Display Name, Username, or First Name - Last Name in the People Picker field of NewForm.aspx is harder than necessary.

Your method varies depending on whether your form is an out of the box (i.e. NewForm.aspx) or a customised SharePoint form (i.e. a new form page created in SharePoint Designer).

This post describes how to pre-populate user details on a customised MyNewForm.aspx page. If you have not customised your page in SharePoint Designer, see How to pre-populate user details on a NewForm.aspx page.

InfoPath Alternative

As an InfoPath Developer it is easy to set the Current User Details in an InfoPath Form (if your SharePoint Installation is on-premise).

SharePoint Online Limitation

If you are using SharePoint Online with Office 365, there is a workaround for finding user details in an Office 365 InfoPath Form. Though you cannot pull down near as many user fields.

Set username on MyNewForm.aspx with JavaScript

To pre-populate user details on a customised SharePoint MyNewForm.aspx page follow these instructions.

Step 1: Prepare the Script

Copy the script below and paste it into Notepad:

<script type="text/javascript">
var context = null;
var web = null;
var currentUser = null;
ExecuteOrDelayUntilScriptLoaded(GetUserLoginName, "sp.js");
function GetUserLoginName() {
context = new SP.ClientContext.get_current();
web = context.get_web();
this._currentUser = web.get_currentUser();
context.load(this._currentUser);
context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),
Function.createDelegate(this, this.onFailureMethod));
}

function onSuccessMethod(sender, args) {
 var today = new Date();
document.getElementById('ctl00_ctl41_g_dd153288_802d_4b13_b21f_c95c4da8deab_ff61_ctl00_ctl00_UserField_upLevelDiv').innerHTML=this._currentUser.get_title();
 //change the ID here to your people picker field ID. Get the id for this field with the help of IE developer tool.
}

function onFailureMethod(sender, args) {
 alert('request failed' + args.get_message() + '\n' + args.get_stackTrace());
 }
</script>

Important: You'll need to replace the highlighted ID with your actual People Picker field ID (see Step 2).

Finding the People Picker Field ID

1

Open MyNewForm.aspx in Internet Explorer

Navigate to your customised form page.

2

Open Developer Tools

Press F12 to open Internet Explorer Developer Tools.

3

Use Select Element Tool

Click on the DOM Explorer Tab and choose the Select Element tool (or press CTRL+B).

Select Element tool in F12 Developer Tools

4

Click the People Picker Field

Using the Select Element tool, click into the Requested By field.

5

Copy the Field ID

Copy the People Picker field ID – in this example the id is"ctl00_ctl41_g_dd153288_802d_4b13_b21f_c95c4da8deab_ff61_ctl00_ctl00_UserField_upLevelDiv"and replace it in the document.getElementById section in your script.

Implementation Steps

1

Edit the Page

Click Settings Cog → Edit page

2

Add Web Part

Click Add a Web Part

3

Select Script Editor

Choose Media and Content → Script Editor, then click Add

4

Insert Script

Click Edit Snippet, paste in your customised code from Notepad, and click Insert

5

Save Changes

Select the Page Ribbon, then click Stop Editing

Success!

Now when a user creates a New Form, their username will populate in the Requested By people picker field.

Method Compatibility

This method will work if you have customised your New Form page (i.e. if you are using MyNewForm.aspx).

It will also work if you have NOT customised your form page. If you have not customised your page you may find this option easier to follow: How to pre-populate user details on a NewForm.aspx page.

Key Differences

  • Uses SharePoint Client Object Model
  • Requires specific field ID targeting
  • Works with customised forms
  • No external libraries needed

Requirements

  • Internet Explorer for debugging
  • F12 Developer Tools access
  • Script Editor Web Part
  • Edit page permissions

Need Help with Custom SharePoint Forms?

Our SharePoint experts can help you customise forms and implement advanced user pre-population techniques for your specific requirements.

Get Custom Development Help