Update People Picker Group field using Rest API and jQuery in SharePoint 2013

sharepoint update person or group field using rest api

This article is to fix the issue in updating / setting the value for People Group field in a list using REST and jQuery in SharePoint 2013.

Issue:

When I was using the below code to set the loan applicant name I was getting the error as “incorrect format filed name not supported”.

sharepoint update person or group field using rest api
sharepoint update person or group field using rest api

Code Snippet:




function updateLoanApplicantDetails(itemTitle, listName, siteUrl, title,lnFstName ,applicantID, success, failure) {
var itemType = GetItemTypeForListName(listName);
var item = {
“__metadata”: { “type”: itemType },
“Title”: title,
“FirstName”:lnFstName,
“applicantNameId”: applicantID
};
getListItemWithTitle(itemTitle, listName, siteUrl, function (data) {
$.ajax({
url: data.__metadata.uri,
type: “POST”,
contentType: “application/json;odata=verbose”,
data: JSON.stringify(item),
headers: {
“Accept”: “application/json;odata=verbose”,
“X-RequestDigest”: $(“#__REQUESTDIGEST”).val(),
“X-HTTP-Method”: “MERGE”,
“If-Match”: “*”
//data.__metadata.etag
},
success: function (data) {
success(data);
},
error: function (data) {
failure(data);
}
});
}, function (data) {
failure(data);
});
}

Issue:

I tried setting the people group value by sending email-id or user-id with domain but there was no luck with neither of them.

Solution:

If we want to update the people group field in SharePoint we have to set the user profile ID of the required / selected user as part of the REST call.



Steps to achieve this:

  1. Provide email-ID or domain of the required user as part of the form
  2. Search whether that user is existed as part of User Profile or not with the below call

var requestUri = _spPageContextInfo.webAbsoluteUrl + “/_api/web/siteusers?$filter=substringof(‘”+$(‘#txtApplnEmlID’).val()+”‘,Email) eq true”;

  1. In the above example I am searching the user with email-ID
  2. If the user is as part of the User-Profile get the ID of the user
  3. Set that user profile ID to people picker control
  4. Follow the below important note
sharepoint update person or group field using rest jquery
sharepoint update person or group field using rest jquery

Note: To set more than one value in people group follow the below syntax:




applicantNameId: {‘results’: [11,12]}
in this example 11, 12 are user profile-ID’s
The complete solution is as follows:
// Applicant Details —————– Start ——————
$(“#btnApplName”).click(function(){
var requestUri = _spPageContextInfo.webAbsoluteUrl + “/_api/web/siteusers?$filter=substringof(‘”+$(‘#txtApplnEmlID’).val()+”‘,Email) eq true”;
$.ajax({
url: requestUri,
type: “GET”,
headers: {
“accept”: “application/json; odata=verbose”
},
success: onSuccess,
error: onError
});

function onSuccess(data) {
var objItems = data.d.results;
var applnEmail = “”;
for (var i = 0; i < objItems.length; i++) {
applnEmail = objItems[i].Id; // Getting the User Profile-ID
}
if(objItems.length>0)
{
updateLoanApplicantDetails($(‘#chEditLnNumers’).find(“:selected”).val(),”loanInformation”,_spPageContextInfo.webAbsoluteUrl, $(‘#txtEdLnID’).val(),$(‘#txtEdFstNm’).val(),$(‘#txtEdLstNm’).val(),$(‘#txtEdLnAmnt’).val(), applnEmail,function(){
alert(“User got Email-ID updated Successfully”);
},function(){
alert(“Error in adding user Details”);
});
}
else
{
alert(“Applicant Email-ID is not valid”);
}
}
function onError(error) {
alert(‘Error’);
}
});

function updateLoanApplicantDetails(itemTitle, listName, siteUrl, title,lnFstName, lnLstName, lnAmnt, applicantID, success, failure) {
var itemType = GetItemTypeForListName(listName);
var item = {
“__metadata”: { “type”: itemType },
“Title”: title,
“FirstName”:lnFstName,
“applicantNameId”: applicantID
};

getListItemWithTitle(itemTitle, listName, siteUrl, function (data) {
$.ajax({
url: data.__metadata.uri,
type: “POST”,
contentType: “application/json;odata=verbose”,
data: JSON.stringify(item),
headers: {
“Accept”: “application/json;odata=verbose”,
“X-RequestDigest”: $(“#__REQUESTDIGEST”).val(),
“X-HTTP-Method”: “MERGE”,
“If-Match”: “*”
//data.__metadata.etag
},
success: function (data) {
success(data);
},
error: function (data) {
failure(data);
}
});
}, function (data) {
failure(data);
});
}
// Applicant Details —————– End ——————

Hope this helps you. Happy development in SharePoint !!!!

Similar SharePoint 2013 Tutorials


Leave a Reply