0

With SPServices I'm getting information from a list A that information contains in one of its columns a people field.

In the form B I need to copy that person coming from the list A in one of the columns that B has that is a people field too.

How could I do that? The format of the information I'm getting form SPServices looks like:

4#;Coello Yngrid

Can I set a people field with that information? Cleaning that field I can get the name of the person.

6

I have used the following javascript to set people pickers on new/edit forms in SP2013:

First you need to get the loginName for the user, you could extract the ID from the value you got and use it to get the loginName through a REST query:

var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
$.ajax({
  url : requestUri,
  contentType : "application/json;odata=verbose",
  headers : requestHeaders,
  success : onSuccess,
  error : onError});

Using the property LoginName returned by the REST query the people picker can be set using the following code (assuming jQuery is available):

var loginName = "i:0#.w|contoso\\johannes"; // Assume this is the returned value from the rest query 
var form = jQuery("table[class='ms-formtable']"); // get the form element
var userField = form.find("input[id$='ClientPeoplePicker_EditorInput']").get(0) // find the people picker element, assuming you dont have multiple people pickers on the form 
var peoplepicker = SPClientPeoplePicker.PickerObjectFromSubElement(userField) // Use SPClientPeoplePicker to get the actual picker object
peoplepicker.AddUserKeys(loginName); // finally set the loginName as the people picker value, this also triggers a validation.
  • 1
    Thank you! It worked. Just a comment: If you have multiple people picker elements you can find your people picker using the Title in your element input[id$='ClientPeoplePicker_EditorInput'] – yngrdyn Apr 16 '15 at 13:18
  • This is the cleanest and simplest solution I've seen. Thanks! – Dinerdo Sep 22 '17 at 16:50
1

Take a look at this link

http://sympmarc.com/2012/04/22/working-with-sharepoint-people-pickers-with-jquery-a-new-function-called-findpeoplepicker/

And use following code to set picker value

siteContactPeoplePicker = $().findPeoplePicker({
    peoplePickerDisplayName: "Site Contact",
    valueToSet: $().SPServices.SPGetCurrentUser()
});
  • Thank you, you pointed me into the right direction. The code in the post didn't work for me though – yngrdyn Apr 16 '15 at 13:17
  • @yngrdyn Ok. on a side note in 2013 we Have REST API. So you don't need SPServices. I would suggest you follow in that line. – Amal Hashim Apr 16 '15 at 13:27

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.