1

I have used below code to Add value in people picker. The code works fine in NewForm.aspx. But when I edit the people picker in EditForm.aspx it doesnt take the new value. It saves the old value only.

function SetNewPeoplePicker(controlName, Username) {
var ppDiv = jQuery("[id$='ClientPeoplePicker'][title='" + controlName + "']");      
var ppEditor = ppDiv.find("[title='" + controlName + "']"); 
var spPP = SPClientPeoplePicker.SPClientPeoplePickerDict[ppDiv[0].id];          
ppEditor.val(Username);                 // Set the value
spPP.AddUnresolvedUserFromEditor(true);                 // Resolve the User if (!spPP.HasInputError) 
{
        var userKeys = spPP.GetAllUserInfo();
        var myUser = userKeys[0];
        Manager = myUser.AutoFillDisplayText;
}
spPP.SetEnabledState(false);
jQuery('a.sp-peoplepicker-delImage').hide();

}

I read that we have to change webpart properties to "Server Rendering". But the whole UI changes and my code written in JS for other field fails.

Can anyone please provide a solution.

Zeba
  • 39
  • 1
  • 9

1 Answers1

3

Since you are using SharePoint 2013 I would recommend you to consider the following 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 and which is a default rendering mode.

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

Below example demonstrates how to initialize User field (AssignedTo field in Tasks List) in New & Edit forms:

Template code:

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

function renderPeoplePickerWithDefaultValue(ctx) {
     //1.set user field value
     var loginName = String.format('i:0#.f|membership|{0}',_spPageContextInfo.userLoginName);  //set to current user login name
     var displayName = $('div#SuiteNavUserName').text();   //set to current user display name 
     var userEntry = createUserEntity(loginName,displayName);
     ctx.CurrentFieldValue = [];   //Note: it is assumed the user field is a multi-valued field (!)
     ctx.CurrentFieldValue.push(userEntry);
     //2.render default People Picker  
     return SPClientPeoplePickerCSRTemplate(ctx); 
}

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

P.S.: in the specified example AssignedTo field is set to current user, in order to specify another user, modify loginName and displayName parameters.

P.P.S. The example has been tested in SharePoint Online

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

References

Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193