3

I want to auto fill the new People picker column in SharePoint 2013. I want to do it in Edit and New form. It could be current user or anyother user.

4
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function SetAndResolvePeoplePicker(fieldName, userAccountName) {
    var controlName = fieldName;
    var peoplePickerDiv = $("[id$='ClientPeoplePicker'][title='" + controlName + "']");
    var peoplePickerEditor = peoplePickerDiv.find("[title='" + controlName + "']");
    var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv[0].id];
    peoplePickerEditor.val(userAccountName);
    spPeoplePicker.AddUnresolvedUserFromEditor(true);
    //disable the field
    spPeoplePicker.SetEnabledState(true);
    //hide the delete/remove use image from the people picker
    //$('.sp-peoplepicker-delImage').css('display','none');
}
function GetCurrentUser(userid) {
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
    var requestHeaders = { "accept" : "application/json;odata=verbose" };
    $.ajax({
        url : requestUri,
        contentType : "application/json;odata=verbose",
        headers : requestHeaders,
        success : onSuccess,
        error : onError
    });
}

function onSuccess(data, request){
    var loginName = data.d.LoginName.split('|')[1];
    //alert(loginName);
    SP.SOD.executeFunc('clientpeoplepicker.js', 'SPClientPeoplePicker', function ()
    {
            //your code goes here
            SetAndResolvePeoplePicker("AssignTo",loginName);
     });

   // return loginName;
}

function onError(error) {
    alert(error);
}
$(document).ready(function () {
    var userid = _spPageContextInfo.userId;
    GetCurrentUser(userid);
});
</script>
0

With respect to Marc D Anderson and his SPServices library:

var currentUser = jQuery().SPServices.SPGetCurrentUser({
    fieldName: "Title",
    debug: false
});

var pickerDisplayName = "Requester Name";

var peoplePicker = jQuery().SPServices.SPFindPeoplePicker({
    peoplePickerDisplayName: pickerDisplayName,
    valueToSet: currentUser,
    checkNames: true
});
0

I use what Taran basically showed, but I found 2 problems in our environment.

Timing - it wouldn't work for some users. I had to call the function with a timing loop, so if it got an error it would retry.

$(document).ready(function() {
   window.setTimeout(function(){ updatePeoplePicker(0,10,200);},200);
});

and in the function, I had to catch the errors are reloop:

function updatePeoplePicker( loopValue,MaxLoops, TimeDelay) {
    try {.... }
    catch(err) {

      loopValue++;

      //Total Time allowed for retrys is 2 seconds
      if(loopValue > MaxLoops)
      {
        window.setTimeout(function(){ doTheDefaults2(loopValue,MaxLoops,TimeDelay);},TimeDelay);
      }
     }
    }

And currently in SP2013, you have to find the editor differently - it now uses a bit different name - it adds , Enter a name or email address... to the field name.

var controlName2 = "Requested By, Enter a name or email address...";    
var ppEditor = ppDiv.find("[title='" + controlName2 + "']");

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.