1) Use REST API url to get the users in people field
2) Write a function where we use ajax to get the data and declare an array and push the existing data along with user you want to add and at last update the listitem column People field.

You can write the code in a button click of a javascript.

Button
======

<input id=”btnUTSubmit” type=”submit” value=”submit” onclick=”insertToList();” style=”text-align: center;width: 100px;margin: 0 auto;display: block;”>

Function
========
Name of the function is: insertToList() and name of list is DOCS and columnname is SchoolUsers

function insertToList() {
var clientContext=SP.ClientContext.get_current();
//Let’s say here item id will be there in URL. We will get it by getting Querystring

// <siteurl>?pdfID=2
JSRequest.EnsureSetup();
itemId=JSRequest.QueryString[“pdfID”];
//REST URL
var urlForAllItems =”/_api/web/lists/getbytitle(‘DOCS’)/items(“+itemId+”)?$select=SchoolUsers/Title&$expand=SchoolUsers/Id”;

//function to get items

getItems(urlForAllItems);
}

Function to get Users and Update
=================================

function getItems(urlForAllItems))
{

$.ajax({

url: “https://abc.hyt/abc&#8221; + url,

type: “GET”,

headers: {

“accept”: “application/json;odata=verbose”,

},

success: function (data) {

var clientContext=SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle(‘DOCS’);
var itemId = getQuerystring(“pdfID”);
var itemToUpdate = oList.getItemById(itemId);
var users =[];

for(var i = 0; i < data.d.SchoolUsers.results.length;i++)
{

users.push(SP.FieldUserValue.fromUser(data.d.SchoolUsers.results[i].Title));

}

users.push(SP.FieldUserValue.fromUser(_sgContext.currentUser.Name));
itemToUpdate.set_item(“SchoolUsers”, users);
itemToUpdate.update();
clientContext.load(itemToUpdate);
clientContext.executeQueryAsync( );

alert(“Thanks for Update the field with new user”);

},

error: function (error) {

alert(JSON.stringify(error));

}

});
}

Advertisement