0

Is there a way to auto-fill people/group field based on a drop down choice for new form? For example, when the user select Choice A, people/group field would automatically fill in "Group A". I use sharepoint 2013 with Javascript. Thank you in advance.

1 Answer 1

0

Please have a try below method:

  1. Create a list and add Choice, people column

enter image description here

  1. Add below JS code:

/* code */

$(document).ready(function () {
    $("select[title='testchoice']").change(function () {
        console.log("Handler for .change() called.");

        if (this.value == "Choice A") {
            SetUserFieldValue("testUser", "SBdev Members");
        }

        if (this.value == "Choice B") {
            SetUserFieldValue("testUser", "SBdev Owners");
        }

    });
});
 
function SetUserFieldValue(fieldName, userName) {
    var _PeoplePicker = $("div[title='" + fieldName + "']");
    var _PeoplePickerTopId = _PeoplePicker.attr('id');

    var _PeoplePickerOject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
    
    while (_PeoplePickerOject.TotalUserCount > 0) {
        _PeoplePickerOject.DeleteProcessedUser();
    }

    var _PeoplePickerEditer = _PeoplePicker.find("input[id$='EditorInput']");
    _PeoplePickerEditer.val(userName);

    _PeoplePickerOject.AddUnresolvedUserFromEditor(true);

}

Result:

enter image description here

*Note: Please not forget to add Jquery refernce when you insert this code (Script editor webpart or JSlink).

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>

More References:

BR

1
  • Hi Thank you very much for your help with the code! Is it possible to check if the person is part of Choice A? If the person is not in Choice A, there would be an alert before submission?
    – Madeline
    Aug 24, 2020 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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