0

I'm new to SP, please be patient with me.

Does anybody know how to auto populate 4 person fields in SharePoint with value from existing item correctly using javascript/jQuery?

What I am trying to do: based on chosen look up value fill in the fields with values from existing (chosen) item - 4 person fields (multi-person fields). The look up contains IDs of existing items. I've found following (sources), but I am not able to make it works not even for one field (error: "Uncaught TypeError: Cannot read property 'get_lookupValue' of null" appears and when I delete the .getlooupValue() then alert displays id+Email e.g. 123JohnDoe@johndoe.com).

Sources:

My 'code' below.

 
$(document).ready(function () {
  var lookUp = $("select[title='LookUp']");
    lookUp.change(function () {
    Populate();
    });
}); 

var ListItem;
function Populate() {
    var chooseLookUp = $("select[title='LookUp']").val();
    var clientContext = new SP.ClientContext.get_current();
    var chooseLookUpList = clientContext.get_web().get_lists().getByTitle('MyListName');
    ListItem = chooseLookUpList.getItemById(chooseLookUp);
    clientContext.load(ListItem);
    clientContext.executeQueryAsync(Function.createDelegate(this,this.GetSet),Function.createDelegate(this,this.Failed));} 

function GetSet(sender, args) {
    //get-set single, multiple line text field, drop-down list, radio button
    $("input[title^='TextField']").val(ListItem.get_item("TextField"));
    $("select[title^='DropDown']").val(ListItem.get_item("DropDown"));
    $("textarea[title^='MultiLine']").val(ListItem.get_item("MultiLine"));
    $("input[id^='Radios'][type='radio'][value='" +  ListItem.get_item("Radios") + "']").prop('checked', true);

    //people picker for person1  
    var context = SP.ClientContext.get_current();
    var web = context.get_web();
    var person1 = web.ensureUser(ListItem.get_item("Person1").get_lookupValue());
    context.load(person1);
    context.executeQueryAsync(function(){
    // Get - Set People Field
    var form = $("table[class='ms-formtable']");
    var person1Field = form.find("input[id$=Person1ID_$ClientPeoplePicker_EditorInput']").get(0);
    var peoplepicker1 = SPClientPeoplePicker.PickerObjectFromSubElement(person1Field); 
    // clear people Picker
    while (peoplepicker1.TotalUserCount > 0) {
      peoplepicker1.DeleteProcessedUser();
    }
    // Set the People field
    var loginName = person1.get_loginName();
    peoplepicker1.AddUserKeys(loginName);},function(sender,args){ // on error
      alert(args.get_message());
    });
 }           

Many thanks

Your Answer

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

Browse other questions tagged or ask your own question.