0

I have a list field named PersoniKonfirmues and I want this field to be set to the current user that is logged in. I want to do this with client side using javascript in Sharepoint 2013

enter image description here

Please help me.

1

Add the below code in Script Editor WebPart in your NewForm

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function () {
    // Wait until SP.JS has loaded before calling getWebUserData 
    ExecuteOrDelayUntilScriptLoaded(SetCurrentUsernameToPeoplePicker, "sp.js");
});

function SetCurrentUsernameToPeoplePicker()
{
var ctx = new SP.ClientContext.get_current();
this.website = ctx.get_web();
this.currentUser = website.get_currentUser();
ctx.load(currentUser);
ctx.executeQueryAsync(Function.createDelegate(this, this.onSucceess), 
Function.createDelegate(this, this.onFail));
}

function onSucceess(sender, args)
 {
var loginName = currentUser.get_loginName();
var form = $("table[class='ms-formtable']"); 
var userField = form.find("input[id$='ClientPeoplePicker_EditorInput']").get(1);
var peoplepicker = SPClientPeoplePicker.PickerObjectFromSubElement(userField);
peoplepicker.AddUserKeys(loginName);
 }

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

[Output] (Tested on My Side)

enter image description here

Note: In case you want to set People Field (not People1), just set get(1) to get(0) if you have multiple People and Picker field just set #OrderIDin get(#OrderID) based on the order ID of your desired People and Picker field on your form

  • making this line of code to the following works perfect var userField = form.find("div[id$='PersoniKonfirmues']").get(0); – Nderon Hyseni Dec 3 '17 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.