How to pre-populate user details in a Customised NewForm.aspx page
- SharePoint
- Posted by: Hayden Sinclair
- font size decrease font size increase font size
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
As an InfoPath Developer it is easy to set the Current User Details in an InfoPath Form (if your SharePoint Installation is on-premise).
If you are using SharePoint Online with Office 365, there is 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 Script Editor Web Part
To pre-populate user details on a customised SharePoint MyNewForm.aspx page follow these instructions.
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 onFaiureMethod(sender, args) {
alert('request failed' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
Go to the MyNewForm.aspx page in Internet Explorer
When using Internet Explorer click F12 to open Developer Tools.
Click on the DOM Explorer Tab and choose the Select Element tool (or CTRL B)
Using the Select Element tool, click into the Requested By field.
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 copy this into the document.getElementById section in notepad.
Click Cog > Edit page
Click Add a Web Part
Choose Media and Content > Script Editor
Click Add
Click Edit Snippet, paste in your code from Notepad, and click Insert
Select the Page Ribbon, then click Stop Editing
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.