1

I'm using Sharepoint 2013. I tried following code to auto populate people picker from current user, but its not working. (Requester AD ID is people picker and account is AD ID of user)

var adid= $().SPServices.SPGetCurrentUser({  
fieldName: "Account",  
});  
$("input[Title='Requester AD ID']").val(adid);
5
  • Does this code gives any error in console? Dec 9, 2016 at 6:41
  • No error in console just people picker field remains empty. Dec 9, 2016 at 6:43
  • try: fieldName: "Account" (delete the comma)
    – Nils
    Dec 9, 2016 at 7:21
  • Tried with removing comma but then no data is fetched Dec 9, 2016 at 8:02
  • Also tried internal name of Account which is Name, but its also not working Dec 9, 2016 at 8:02

4 Answers 4

2

Please check below code.

$(document).ready(function() {
var userName= $().SPServices.SPGetCurrentUser();
$().SPServices.SPFindPeoplePicker({
   peoplePickerDisplayName: "Owner",
   valueToSet: userName,
   checkNames: true
  });
});
2
  • Not working. No error but people picker field remains empty. Dec 9, 2016 at 12:52
  • I'm applying this code on NewForm.aspx and this field is clientpeoplepicker autocomplete field. Dec 9, 2016 at 12:53
0
var usr;
var contextDR;
var webDR;

$(document).ready(function () 
{
     initializePeoplePicker('peoplePickerteamleaderDiv');
     SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getusername);
});

//initialize PeoplePicker div tag
function initializePeoplePicker(peoplePickerElementId) 
{
     var schema = {};
     schema['PrincipalAccountType'] = 'User,DL,SecGroup,SPGroup';
     schema['SearchPrincipalSource'] = 15;
     schema['ResolvePrincipalSource'] = 15;
     schema['AllowMultipleValues'] = true;
     schema['MaximumEntitySuggestions'] = 50;
     schema['Width'] = '222px';
     this.SPClientPeoplePicker_InitStandaloneControlWrapper(peoplePickerElementId, null, schema);
}


//get plannedby user
function getusername() 
{
     contextDR = new SP.ClientContext.get_current();
     webDR = contextDR.get_web();
     usr = webDR.get_currentUser();
     contextDR.load(usr);
     contextDR.executeQueryAsync(Function.createDelegate(this, function () { usrsuccess(); }), Function.createDelegate(this, function () { usrfail(); }));
}

function usrsuccess(sender, args) 
{
     var a = usr.get_title() + ";";
     var peoplePickerteamleaderDiv = this.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerteamleaderDiv_TopSpan;
     peoplePickerteamleaderDiv.AddUserKeys(a);
}

function usrfail(sender, args) 
{
}

body part:

<div id="peoplePickerteamleaderDiv"></div>

use this js reference:

_layouts/15/clienttemplates.js
_layouts/15/clientpeoplepicker.js
_layouts/15/clientforms.js
_layouts/15/autofill.js
_layouts/15/1033/strings.js
1
  • I want to set value in shrepoint NewForm.aspx,where people picker is already availabe. If i create another people picker then I have to save its data dynamically, and i dont want to do that. Dec 12, 2016 at 13:15
0

The following is the full code that works for me in SharePoint Online. I placed this immediately after PlaceHolderMain in the NewForm.aspx page of my list in SharePoint Designer:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">
</script>
<script src="/_layouts/15/clientforms.js"></script>
<script src="/_layouts/15/clientpeoplepicker.js"></script>
<script src="/_layouts/15/autofill.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.02/jquery.SPServices.js"></script>
<script type="text/javascript">

$(document).ready(function () {
  SP.SOD.executeFunc('sp.js', null, function () {
     retrieveUserInfo("Employee");  // Set this to the Title of the person column
  })
});

function retrieveUserInfo(PickerTitle) {
    var currentuserName = $().SPServices.SPGetCurrentUser({
        fieldName: "Title"
    });

    $('input[title="' + PickerTitle + '"]').val(currentuserName).attr('size', 40);

    $('div[title="' + PickerTitle + '"] span.ms-helperText').hide();

    // Select the target user field from the dictionary of user fields on the page.
    var peoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[$('[title="' + PickerTitle + '"]')[0].id];

    // Resolve the user using the value set in the input field.
    peoplePicker.AddUnresolvedUserFromEditor(true);
}
0

Check this question the answer given by WWC. (worked for me - SharePoint Online)

try {
    //SharePoint 2013
    var travelPeoplePicker = null;
    for (var prop in SPClientPeoplePicker.SPClientPeoplePickerDict) {
        if (prop.indexOf("YOUR_PEOPLEPICKER_FIELD_NAME_HERE") >= 0) {
            travelPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[prop];
            break;
        }
    }
    if (travelPeoplePicker != null) {

        var usrObj = { 'Key': strUserName };
        travelPeoplePicker.AddUnresolvedUser(usrObj, true);
        //$("input[title='TravelRequestor']").focus().val(strUserName).attr("size", strUserName.length);
    }
}
catch (e) {
    //alert(e);
}    

Your Answer

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

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