The correct way to write a SharePoint user to a user field in a SharePoint list

I am writing a user to a SharePoint list.

I read that the SharePoint user field contains a line like this inside it: userId;#userLoginName

I tried formatting in the same way when writing to the User field, for example, when I write this line, it works: 9;#i:0#.f|membership|ectropy@example.org

But what is strange (at least for me) is that 9;#it seems to work or even 9. Even if I donโ€™t transmit the information userLoginNameat all loginId, it seems to be enough to find out which user I'm talking to.

This, apparently, means that when writing to a SharePoint user field, you only need the identifier userLoginName, and, indeed, everything after loginIddoes not matter.

Am I correcting my reasoning here? Or maybe there were unforeseen consequences if I left userLoginName information?

+5
source share
2 answers

Your assumption is true, only a user identifier is a required property when specifying a value for the User field.

But since the SP.FieldUserValue object is used to store the value of the User field, it is recommended to get and set the values โ€‹โ€‹using this object, as shown in the example below:

var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var lists = web.get_lists();
var list = lists.getByTitle(listTitle);
var item = list.getItemById(itemId);

var assignedToVal = new SP.FieldUserValue();
assignedToVal.set_lookupId(11);   //specify User Id 
item.set_item(fieldName,assignedToVal);
item.update();

ctx.executeQueryAsync(
    function() {
        console.log('Updated');
    },
    function(sender,args) {
        console.log('An error occurred:' + args.get_message());
    }
);
+7
source

, , , .

https://gist.github.com/rheid/18635032d8371c7825b3320eae57071f

// Single Person  
   var singleUser = SP.FieldUserValue.fromUser('Peter Dotsenko');  
   oListItem.set_item('PetkaPersonSingle', singleUser);  

   //Multi Person  
   var petkaUserMultiArray = new Array("peterd@domain.com","Peter Dotsenko","domain\\peterd");  
   var lookups = [];  
   for (var ii in petkaUserMultiArray) {  
      var lookupValue = SP.FieldUserValue.fromUser(petkaUserMultiArray[ii]);  
      lookups.push(lookupValue);  
   }  
   oListItem.set_item('PetkaPersonMulti', lookups);
//
0

Source: https://habr.com/ru/post/1544976/


All Articles