I'm using Javascript to update my fields in a SharePoint list via Rest requests.
I have several columns in my list representing different people (some multiple people columns, some single).
When updating a multiple-user type column, I have no problem passing back an array of the ID's I want in the array to update. If I want to remove all of the people, I simply post an empty array in the column and the field is essentially null.
This works well with an option list with multiple users:
//Note, this is inside a function that gives me the "ThisPhase", "ControlToUpdate" and "ThisColumn" objects.
var UsersSelected = []; //this is the array we save back to sharepoint for the current column we're editing.
var UserNames = ""; // this is the text we save to the log
var UsersData = []; // this is the data we put back in the row for the redraw of the table
$('#CURRENT_USER_LIST option').each(function(){
UsersSelected.push(parseInt($(this).val()));
UserNames += $(this).text() + ", ";
UsersData.push({
Id: parseInt($(this).val()),
Title: $(this).text()
});
});
var ItemProperties = {};
ItemProperties[ThisColumn] = {
'results': UsersSelected
};
if(UsersSelected.length > 0){
//we have at least one user assigned
ThisPhase[ThisColumn] = {
"results": UsersSelected
};
}
else{
//nobody assigned
ThisPhase[ThisColumn] = {
"results": null
};
};
updateListItem(_spPageContextInfo.webAbsoluteUrl,'PROJECT%20PHASES',ThisPhase.Id,ItemProperties,
function(data){//successfully updated.
if(UsersData.length > 0){
UserNames = "";
for (var c = 0; c < UsersData.length; c++) {
UserNames += UsersData[c].Title + (UsersData.length > c+1 ? ", ": "");
};
};
$(ControlToUpdate).html(UserNames);
},logError);
The problem comes when I attempt to pass a null value back on a single user column. when I pass a null value into the "ThisColumn" (which is a single user column):
var UserSelected = 0; //this is the array we save back to sharepoint.
var UserName = ""; // this is the text we save to the user log
var UsersData; // this is the data we put back in the row for the redraw of the table
UserSelected = $('#SINGLE_NEW_USER_LIST').val() || null;
UserName = UserSelected ? $('#SINGLE_NEW_USER_LIST :selected').text() : "";
UserData = {
Title: UserName
};
var ItemProperties = {};
ItemProperties[ThisColumn] = UserSelected;
updateListItem(_spPageContextInfo.webAbsoluteUrl,'PROJECT%20PHASES',ThisPhase.Id,ItemProperties,
function(data){ //successfully added information
ThisPhase[ThisColumn] = UserSelected;
UserName = UserSelected ? UserName : "[CLICK TO ADD "+ColumnToDisplayText+"]";
$(ControlToUpdate).html(UserName);
},
function(err){
console.log(err);
}
);
I get the following error back from SharePoint :
"Cannot convert a primitive value to the expected type 'Edm.Int32'. See the inner exception for more details."
I've been searching for quite a while, but I can't seem to find any answers on how to use a POST request to REMOVE an ID from a Single-user field in SharePoint .
Any help would be much appreciated.
updateListItem
method look like? You might need to use a DELETE request instead of a POST request.