2

I've seen a LOT of posts about this, and I am sorry to add to them, but I have spent a lot of time going through them and cannot find an answer that is working in my case.

My objective is to populate a PeoplePicker field with the current user via jQuery/JavaScript and SPServices, if possible.

My first and most obvious attempt was to use the SPServices function $().SPServices.SPFindPeoplePicker({ ... });, however, this function does not seem to be working or I am not understanding how it is supposed to work.

Here is my attempt:

var currUser = $().SPServices.SPGetCurrentUser({ fieldName: "Name" }).split('|')[1];
$().SPServices.SPFindPeoplePicker({
  peoplePickerDisplayName: "Assigned To",
  valueToSet: currUser,
  checkNames: true
});

The above code is not working for me. I have confirmed that the SPFindPeoplePicker function is selecting the correct PeoplePicker field by posting the response to the console, but the valueToSet parameter is being ignored. I've tried passing it a user title, and hard coding a specific user to ensure there is not an issue with the currUser variable.

Could somebody explain what is going wrong with this function? Am I misunderstanding how this is supposed to function?

Thank you for your help!

  • you are doing nothing wrong, the rendering of people picker has changed in SharePoint 2013. So I use this walkaround, I hope it helps you link – Marco Dec 5 '14 at 20:59
  • Hi @Marco , Thank you for your response. I was guessing that might be the case, but couldn't find it confirmed in my research. However, I don't see a workaround on the link you provided. It appears to be a clone of the existing ().SPServices.SPFindPeoplePicker function. I'm basically getting the exactly the same response. – Sunburnt Ginger Dec 6 '14 at 0:20
  • what is "Assigned To" here?. ID of PeoplePicker control? – Harsh Dec 19 '17 at 7:11
2

Since you are using SharePoint 2013 I would recommend you to consider a different approach, in SharePoint 2013 was introduced a so called Client Rendering Mode (CSR) which is intended for rendering of List Views and Forms using HTML and JavaScript. To get acquainted with CSR follow these articles:

How to initialize User field in New/Edit forms using CSR

Below example demonstrates how to populate User field (AssignedTo) in New form for a List (Tasks list)

Template code:

   (function () {
    var ctx = {};
    ctx.Templates = {};
    ctx.Templates.Fields = {
        'AssignedTo': {
            'NewForm': renderAssignedTo
        }
    };
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(ctx);
})();

function renderAssignedTo(ctx) {
     //get current user properties
     var displayName = $('div#SuiteNavUserName').text();
     var loginName = String.format('i:0#.f|membership|{0}',_spPageContextInfo.userLoginName);
     var currentUserEntry = createUserEntity(loginName,displayName);
     //Set user default value
     ctx.CurrentFieldValue = [];   //Note: it is assumed the user field is a multi-valued field (!)
     ctx.CurrentFieldValue.push(currentUserEntry);  
     return SPClientPeoplePickerCSRTemplate(ctx);
}

function createUserEntity(userLoginName,userDisplayName)
{
   return {
      Description: userLoginName,
      DisplayText: userDisplayName,
      EntityGroupName: "",
      EntityType: "",
      HierarchyIdentifier: null,
      IsResolved: true,
      Key: userLoginName,
      MultipleMatches: [],
      ProviderDisplayName: "",
      ProviderName: ""
   };
}

How to apply the changes

There are at least two options how to apply the changes:

  1. Using JSLink property
  2. Place JavaScript template on page via Script Editor/Content Editor web parts

Here is how to apply the changes using the second option:

  1. Switch the page (NewForm.aspx) into edit mode
  2. Add Script Editor webpart right below the list view web part.
  3. Put the specified code by wrapping it using script tag code into the Script Editor, for example: <script type="text/javascript">{Template JS code goes here}</script>
  4. Save the page

Results

enter image description here

  • 2
    40 lines of code to add a string to one field. thanks, microsoft – ferr Mar 28 '17 at 15:49
1

As mentioned in the comment to my question, the SPFindPeoplePicker function currently does not work for SharePoint 2013. However, I was able to find an alternative solution and posted it on my other question here.

  • Please add code snippets from the referred blog – Taran Goel Aug 29 '15 at 18:38
  • Your link worked for me. calling AddUnresolvedUserFromEditor on the peoplepicker instance did the trick. – ferr Mar 28 '17 at 15:56

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.