0

How to set person field on form load and save the field value when item saves. I have added following code in script editor web part NewForm.aspx. But this is not working. Appreciate your support.

$(document).ready(function () {
    $("div[title='Assigned To']").val("i:0#.w|domain\user");
});
  • 1
    Try using the solution given here and let me know if it works for you. – Ganesh Sanap Jan 16 at 11:04
1

Try using below code:

//Display name of your field
var fAssignedTo = "Assigned To";  

$(document).ready(function(){  
     BindPeoplePickerWithLoggedinUser(fAssignedTo);  
});

function BindPeoplePickerWithLoggedinUser(ppTitle){  
    setTimeout(function(){  
        var _PeoplePicker = $("div[title='" + ppTitle + "']");  
        var peoplePickerEditor = _PeoplePicker.find("[title='" + ppTitle + "']");  
        var _PeoplePickerTopId = _PeoplePicker.attr('id');

        //This is setting the value to current logged in user
        peoplePickerEditor.val(_spPageContextInfo.userDisplayName);  
        var ppobject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];         
        ppobject.AddUnresolvedUserFromEditor(true);  
    }, 1000);      
}

Reference:

  1. People Picker Field Actions In SharePoint Using JavaScript (JSOM).
| improve this answer | |
  • It's not working for me. – Sivakumar Piratheeban Jan 16 at 14:55
  • 1
    Are you getting any error in console? Are you passing the field name correctly? – Ganesh Sanap Jan 16 at 15:40
  • I don't get any error in console. Field name is correct – Sivakumar Piratheeban Jan 17 at 4:04
  • Strange. This is working for me and I was able to add the list item. Are you passing the current logged in user (same as given in my answer)? – Ganesh Sanap Jan 17 at 4:08
1

Use

SP.SOD.executeFunc('clientpeoplepicker.js', 'SPClientPeoplePicker', function ()
{
    var neededPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[GiveTheIndexHere];
    var usrObj = { 'Key': 'i:0#.w|domain\user' };
    neededPeoplePicker.AddUnresolvedUser(usrObj, true);
})

Adding my complete code below. The requirement was to auto-populate L1, L2, L3 managers based on the user name added to the 'Process Owner' field.

$(document).ready(function()
{
    SP.SOD.executeFunc('clientpeoplepicker.js', 'SPClientPeoplePicker', function ()
    {
        var processOwnerFieldIdProcessOwner = $("div[title='Process Owner']").attr('id');

        SPClientPeoplePicker.SPClientPeoplePickerDict[processOwnerFieldIdProcessOwner].OnControlValidateClientScript = function (peoplePickerId, selectedUsersInfo)
        {
            console.log('inside OnValueChangedClientScript');
            var processOwnerFieldUserInfoJson = SPClientPeoplePicker.SPClientPeoplePickerDict[processOwnerFieldIdProcessOwner].GetAllUserInfo();

            if(processOwnerFieldUserInfoJson.length > 0 )
            {
                var processOwnerFieldUserId = processOwnerFieldUserInfoJson["0"].AutoFillKey;
                processOwnerFieldUserId = encodeURIComponent(processOwnerFieldUserId);
                console.log(processOwnerFieldUserId);
                getManagersOfUser(processOwnerFieldUserId);
            }
        };


        function getManagersOfUser(processOwnerFieldUserId)
        {
            $.ajax({

            url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='"+processOwnerFieldUserId+"'",

            method: "GET",

            headers:
                {  
                    "accept": "application/json;odata=verbose",
                    "content-type": "application/json;odata=verbose"
                },  

            success: function(data)
                {  
                    console.log(data);
                    populateL1L2L3Managers(data.d.ExtendedManagers.results);
                },

            error: function(error)
                {  
                    console.log(JSON.stringify(error));  
                }  
            })
        }       


        function populateL1L2L3Managers(managersArray)
        {   
            var counter = 1;
            for(var i = managersArray.length-1; i> managersArray.length-4 && i >= 0;i--)
            {
                console.log(managersArray[i]);
                //debugger;
                var peoplePickerName;

                if(counter == 1)
                {
                    peoplePickerName = "L1_x0020_Manager";
                }
                else if(counter == 2)
                {
                    peoplePickerName = "L2_x0020_Manager";
                }
                else if(counter == 3)
                {
                    peoplePickerName = "L3_x0020_Manager";
                }

                counter++;

                try
                {
                        var travelPeoplePicker = null;
                        for (var prop in SPClientPeoplePicker.SPClientPeoplePickerDict)
                        {

                            if (prop.indexOf(peoplePickerName) >= 0)
                            {
                                travelPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[prop];
                                break;
                            }
                        }

                        if (travelPeoplePicker != null)
                        {
                            travelPeoplePicker.DeleteProcessedUser();

                            var usrObj = { 'Key': managersArray[i] };
                            travelPeoplePicker.AddUnresolvedUser(usrObj, true);
                        }
                    }
                    catch (e)
                    {
                        //alert(e);
                    }       
            }       
        }



    });
});
| improve this answer | |

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.