0

For one of the implementations, I am using a basic SharePoint list form to add entries in a list, there is a workflow attached to it as well. On NewForm.aspx, I have a column called 'Requester' (people picker field) which I would like to set to Current user when the form is opened. I have seen couple of solutions using SPServices for this type of question but I do not want to leverage SPServices, is there a different approach that someone took and works, if so please share the code.

FYI, I am working with SharePoint online. Thanks in advance.

1

I do it like this in SP2013 on-prem (online might be slightly different?):

$(document).ready(function () {
    SP.SOD.executeOrDelayUntilScriptLoaded(loadCurrentUser, 'clientpeoplepicker.js');
});

function loadCurrentUser() {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
        var currentUserRequest = $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + '/_api/web/currentuser',
            method: 'GET',
            headers: {
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose"
            }
        });
        currentUserRequest.done(function (result) {
            var peoplePickerCtrl = $('nobr:contains("Display Name of Field")').closest('tr').find('div.sp-peoplepicker-topLevel')
            var peoplePickerCtrlId = $(peoplePickerCtrl).attr('id');
            var peoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerCtrlId];
            var userObj = { 'Key': result.d.Email };
            peoplePicker.AddUnresolvedUser(userObj, true);
        });
    });
}
  • Works in SPOnline as well, thanks so much Dylan, appreciate the help. – mdevm Dec 20 '18 at 19:16

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.