Rest api create sharepoint list item
-
08-02-2021 - |
Вопрос
i am writing code where i am updating rows from excel for which i have used REST api but few of my column in sharepoint are choice , persongroup,date type, when i am trying to create item for that its no working below is sample code:
function createEmployeeDetailsListItem(excelRow) {
$.ajax
({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Test')/items",
type: "POST",
data: JSON.stringify
({
__metadata:
{
type: "SP.Data.E2EVMVulnerabilitiesMappingMasterListItem"
},
Title: excelRow["Title"],
ID:excelRow["ID"],
DateCol:excelRow[""], //Datecolumn
PersonGroupCol:excelRow[""], //Persongroupcolumn
ChoiceCol:excelRow[""] //Choicecolumn
}),
headers:
{
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "POST"
},
success: function (data, status, xhr) {
console.log("success");
},
error: function (xhr, status, error) {
console.log("error");
}
});
}
Решение
See the updated sample code as below:
If you have single selection for User and Group field and for Choice field then create data as given below
function createEmployeeDetailsListItem(excelRow) {
var data = {
__metadata: {
type: "SP.Data.E2EVMVulnerabilitiesMappingMasterListItem"
},
Title: excelRow["Title"],
ID: excelRow["ID"],
DateCol: new Date(excelRow["Datecolumn"]).toISOString(), //Datecolumn
PersonGroupColId: excelRow["Persongroupcolumn"], //here you need to set the Id of the Group or a User (For single user or group selection)
ChoiceCol: excelRow["Choicecolumn"] //Choicecolumn, here you need to set you Choice as a Stirng "Choice 1" (Only work for single Choice selection)
}
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Test')/items",
type: "POST",
data: JSON.stringify(data),
headers: {
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "POST"
},
success: function(data, status, xhr) {
console.log("success");
},
error: function(xhr, status, error) {
console.log("error");
}
});}
If you have Multi selection for User and Group field and for Choice field then create data as given below
function createEmployeeDetailsListItem(excelRow) {
var data = {
__metadata: {
type: "SP.Data.E2EVMVulnerabilitiesMappingMasterListItem"
},
Title: excelRow["Title"],
ID: excelRow["ID"],
DateCol: new Date(excelRow["Datecolumn"]).toISOString(), //Datecolumn
PersonGroupColId: { 'results': excelRow["Persongroupcolumn"] }, //Here excelRow["Persongroupcolumn"] is array of multiple choice - When you field set as Allow Multiple
ChoiceCol: {
"__metadata": {
"type": "Collection(Edm.String)"
},
"results": excelRow["Choicecolumn"] //Here excelRow["Choicecolumn"] is array of multiple choice - When you field set as Allow Multiple
}
}
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Test')/items",
type: "POST",
data: JSON.stringify(data),
headers: {
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "POST"
},
success: function(data, status, xhr) {
console.log("success");
},
error: function(xhr, status, error) {
console.log("error");
}
});}
Не связан с sharepoint.stackexchange