How to update list item with lookup (People and Group) column using REST post method
문제
I want to update a list item with People and Group column.
The list has 4 columns:
- Title
- Description
- ProsessOwner
- ProsessContributer
I am not able to add/update through a REST POST method, since I am getting an error on these two columns: ProsessOwner, ProsessContributer. If i remove them both from the query string, the rest query works.
This is the returned error:
A node of type 'PrimitiveValue' was read from the JSON reader when trying to read the entries of a feed. A 'StartObject' or 'EndArray' node was expected.
And my code :
$scope.saveNewPage = function () {
var err = false;
var title = $scope.newPage.title;
var descr = $scope.newPage.description;
var pOwnerID = { 'results': [processOwnerID,11] };
var pContrID = { 'results': [processContributerID,11] };
var data = { "Title": title, "Description": descr, "ProsessOwner": pOwnerID, "ProcessContributer": pContrID };
ppService.addnewPage(data).then(function (data) {
close(data.d.Id);
}, function (error) {
console.log('Feilet med å opprette ny Side');
});
}
var addnewPage = function (data) {
var addData = {
__metadata: { 'type': 'SP.Data.GNPagesListItem' },
Title: data.Title,
Description: data.Description,
ProsessOwner: data.ProsessOwner,
ProcessContributer: data.ProcessContributer
};
var url = listEndPoint + "/GetByTitle('GNPages')/Items";
return baseSvc.postRequest(addData, url);
};
var postRequest = function (data, url) {
var deferred = $q.defer();
$http({
url: baseUrl + url,
method: "POST",
headers: {
"content-Type": "application/json;odata=verbose",
"accept": "application/json;odata=verbose",
"X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
},
data: JSON.stringify(data)
})
.success(function (result) {
deferred.resolve(result);
})
.error(function (result, status) {
deferred.reject(status);
console.log(result)
});
return deferred.promise;
};
Edit
I just edited my list "Person or Group" column setting to only allow only single value selection and the REST query worked fine with the code below:
var addnewPage = function (data) {
var addData = {
__metadata: { 'type': 'SP.Data.GNPagesListItem' },
Title: data.Title,
Description: data.Description,
ProsessOwnerId: 10,
ProcessContributerId: 11
};
var url = listEndPoint + "/GetByTitle('GNPages')/Items";
return baseSvc.postRequest(addData, url);
};
But as soon as I changed column back to the previous setting ( allow multiple selections), and change the REST query to send an array of id's like [10,11], I get the same error:
A node of type 'PrimitiveValue' was read from the JSON reader when trying to read the entries of a feed. A 'StartObject' or 'EndArray' node was expected.
해결책
I guess you are missing something in this step.
var addData = {
__metadata: { 'type': 'SP.Data.GNPagesListItem' },
Title: data.Title,
Description: data.Description,
ProsessOwnerId: data.ProsessOwner,
ProcessContributerId: data.ProcessContributer
};
To update a people or group field, instead of ProsessOwner or ProcessContributer the nomenclature should be ProsessOwnerId and ProcessContributerId. Most of us will miss the suffix 'Id' with the internal name while updating a people or group filed. And the value for the field should be User Id of the user in the Site.
다른 팁
Seems like there is some mismatch in your JSON or data variable.
Please check whether for "data.ProsessOwner" and "data.ProcessContributer" you are getting correct ID's.
please refer this answer - Updating Lookup Values with the REST API
here is a solution for you, in scenarios you want to update a people column [mypeoplecol] through rest / spfx:
- Single value people column:
[mypeoplecol]Id = X
, where X is a [number] type value. - Multiple value people column:
[mypeoplecol]Id = {results:[X, Y, ...]}
, where X, Y, ... are [number] type value.
Take into account that you may need to leverage the
ensureUser
service, in order to ensure the target(s) user(s) do have and ID on that site.