Get Current User and update in InfoPath Form Column

Total visits : 406

Get Current User data using jQuery/JavaScript

Add a Content editor webpart in InfoPath form New page, add the below code

<script type=”text/javascript” language=”javascript”>

var user;
var email;
var firstname;
var surname;

function getUserInfo()

var clientContext = new SP.ClientContext();
user = clientContext.get_web().get_currentUser();
clientContext.load(user);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

}

function onQuerySucceeded() {
var UserName = user.get_title();
var UserNamearray = UserName.split(” “);
firstname= UserNamearray[0];
surname= UserNamearray[1];
email = user.get_email();

$(“input[title=’First Name’]”).val(firstname); //title is text box tool tip property
$(“input[title=’First Name’]”).focus(); // set focus is very important to retain the value in the form

$(“input[title=’Sur Name’]”).val(surname);
$(“input[title=’Sur Name’]”).focus();

$(“input[title=’Email’]”).val(email);
$(“input[title=’Email’]”).focus();

}

function onQueryFailed(sender, args) {
alert(‘Request failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
}
$( document ).ready(function(){
ExecuteOrDelayUntilScriptLoaded(getUserInfo, “sp.js”);
});

Point to remember when set the text with InfoPath form text box

  1. .val to set the value
  2. set focus() is very important, otherwise it will remove when any dropdown or richtextbox column sending data to server.
  3. In InfoPath form, Text box property –> Browser forms tab –> set the postback options of the field to NEVER , refer image below.

textboxproperty
This way we can call JSON Rest API’s and update the InfoPath form columns.