1

I have a list "List1" having following fields

Department : Single line of text

HOD : Person or group

There is another list called "List2" with following fields

FacultyName : Single line of text

Dept : Lookup ("Department" column of "List1")

HOD : Person or Group

My query is how to auto-populate HOD field in "List2" based on the "Dept" lookup selection.

It should fetch value from corresponding HOD field of "List1".

I am using SharePoint online.

Please help me out here. Thanks in advance.

3
  • I have updated the code. Did you try it? Apr 19, 2018 at 9:22
  • Thanks JoannaW_MSTF for the answer. It worked. But there is slight change in requirement. There won't be group, instead multiple values will be present in the HOD field. Can you tell how to retrieve multiple values from person field?
    – sandeep
    Apr 25, 2018 at 6:01
  • You need to iterate the HOD values and get each one person, such as ListItem.get_item("HOD")[i].get_lookupValue(). You can refer to stackoverflow.com/questions/22694749/…. For the new requirement, you can post a new thread as this thread is old. Accept the answer if it is helpful for the original requirement in this thread. Apr 25, 2018 at 7:32

1 Answer 1

1

Try adding the following script to the from. It will auto-populate the People Picker column based on the lookup column.

<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script language="javascript">

$(document).ready(function () {
// the lookup field in the new form
 var IssueIDField= $("select[title='Dept']");
 IssueIDField.change(function () {
 Populate();
});
}); 

var ListItem;
function Populate() {
 var IssID = $("select[title='Dept']").val();
 var clientContext = new SP.ClientContext.get_current();
 var IssueList = clientContext.get_web().get_lists().getByTitle('cl01');
 ListItem = IssueList.getItemById(IssID);
 clientContext.load(ListItem);
 clientContext.executeQueryAsync(Function.createDelegate(this,this.Succed),Function.createDelegate(this,this.Failed));
} 

function Succed(sender, args) {

var context = SP.ClientContext.get_current();
var web = context.get_web();
var user = web.ensureUser(ListItem.get_item("HOD").get_lookupValue());

// Get People Field
var form = $("table[class='ms-formtable']");
var userField = form.find("input[id$='HOD_17d85e7c-80fe-4131-8b50-c5d456fdd449_$ClientPeoplePicker_EditorInput']").get(0);
var peoplepicker = SPClientPeoplePicker.PickerObjectFromSubElement(userField);
// clear people Picker
while (peoplepicker.TotalUserCount > 0) {
 peoplepicker.DeleteProcessedUser();
}

context.load(user);
context.executeQueryAsync(function () {
    //execute successfully
    //this is a user field
    // Set the People field
    var loginName = user.get_loginName();
    peoplepicker.AddUserKeys(loginName);
}, function (sender, args) {
    //execute fail, so it isn't user field, it may be a group
    var collGroup = context.get_web().get_siteGroups();
    //get the people group via Id
    var oGroup = collGroup.getByName(ListItem.get_item("HOD").get_lookupValue());
    context.load(oGroup);
    context.executeQueryAsync(
        //execute successfully
        //this is a group field
        function () {
            // Set the People field
            var loginName = oGroup.get_loginName();
            peoplepicker.AddUserKeys(loginName);
        },
         //execute fail, it is neither user nor group
        function (sender, args) {
            contoso.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        })
});
 }
function Failed(sender, args) {
 console.log('Error. ' + args.get_message() + '\n' + args.get_stackTrace());
 }
</script>

Change the 'cl01' to your 'List1'. And change 'HOD_17d85e7c-80fe-4131-8b50-c5d456fdd449_$ClientPeoplePicker_EditorInput' to the id of your HOD column in 'List2'.

Use F12 to check the id of the element.

enter image description here

Refer to Auto-populate list form fields based on lookup selection in SharePoint.

3
  • Thank you, It works. Can you please tell me how to apply the same solution in case of "Group". (Above solution works if the value is the name of a person, but now I am asking for groups. )
    – sandeep
    Apr 17, 2018 at 9:47
  • I updated the code. It also works for group. Apr 18, 2018 at 2:59
  • sharepoint.stackexchange.com/questions/240888/… @JoannaW_MSTF This is my new question for Multiple Person or group values fetching. Please answer to it. Thanks in advance
    – sandeep
    Apr 30, 2018 at 7:15

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.