QuiXXotic
QuiXXotic

Reputation: 3

Updating SP.ListItem with SP.Field Value from SP.PeoplePicker

Im trying to update an SP.Listitem withholding an spUser with another user with the use of JSOM. See codesnippet bellow

// Query the picker for user information.
$.fn.getUserInfo2 = function () {
  var eleId = $(this).attr('id');
  var siteUrl = _spPageContextInfo.siteServerRelativeUrl;
  var spUsersInfo = GetPeoplePickerValues(eleId);
  var clientContext = new SP.ClientContext(siteUrl);
  var oList = clientContext.get_web().get_lists().getByTitle('VLS-dokument');
  var itemArray = [];

  for(i=0;i<$.miniMenu.i.results.length;i++)
  {
    var item = $.miniMenu.i.results[i];

    var oListItem = oList.getItemById(item.Id);
    oListItem.set_item('Informationsägare', SP.FieldUserValue.fromUser(spUsersInfo.Key));
    oListItem.update();
    itemArray.push(oListItem);
    clientContext.Load(itemArray[itemArray.Length - 1]);
  }

  clientContext.executeQueryAsync(Function.createDelegate(this, function () { alert(""); }), Function.createDelegate(this, function () { alert(""); }));

  return spUsersInfo; //.slice(0, -2)
}

spUsersInfo contains the user obj, peoplePicker.GetAllUserInfo()

The return off SP.FieldUserValue.fromUser(spUsersInfo.Key) to be the problem since the app crash reaching that line oListItem.set_item('Informationsägare', SP.FieldUserValue.fromUser(spUsersInfo.Key));

What part of the user obj is supposed to be passed into SP.FieldUserValue.fromUser(spUsersInfo.Key) if not the key? Is there another way to do it?

Upvotes: 0

Views: 3050

Answers (1)

Thriggle
Thriggle

Reputation: 7059

People picker columns are really just lookup columns, looking up against the site collection's user information list. You can set a lookup column either by specifying the ID of the desired item in the lookup list, or by creating a special lookup field value object (letting SharePoint do the work of finding the ID, given the desired text value).

According to the documentation the value passed to SP.FieldUserValue.fromUser() should be the user's name as a string. In practice, this should be the user's display name from the user information list.

So if you don't know the user's lookup ID, but do know their display name, you would use this: oListItem.set_item('Informationsägare',SP.FieldUserValue.fromUser(username));

If you didn't know the user name, but did know the lookup ID of the user, you could instead pass that number to item.set_item() directly, i.e. oListItem.set_item('Informationsägare',lookupId);.

If the spUsersInfo.Key value from your GetPeoplePickers() method is in the format of i:0#.w|Cool Person then you can split that value and just get the string Cool Person to feed into SP.FieldUserValue.fromUser().

Upvotes: 2

Related Questions