Cannot insert List item with 400 Bad request as response in SharePoint REST
1. Create a SharePoint List. Create a column name start with number.
2. Write a REST code and upload file into Site Assets.
File: RESTDemo.html
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#btnCreateNewItem").click(function() {
funCreateNewItem();
});
});
function funCreateNewItem() {
//https://sharepointonline01.sharepoint.com/sites/dev2/_api/web/lists/GetByTitle('TestList01')/items"
//https://sharepointonline01.sharepoint.com/sites/dev2/_api/web/lists/GetByTitle('TestList01')/fields"
var listName = "TestList01";
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('"+listName+"')/items",
type: "POST",
data: JSON.stringify
({
__metadata:
{
type: "SP.Data."+listName+"ListItem"
},
Title : "Title
01",
_x0036__x0020_Digit_x0020_Number : "123456" //Error
//OData__x0036__x0020_Digit_x0020_Number: "123456" //Correct
}),
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");
}
});
}
</script>
</head>
<body>
<button type="button" id="btnCreateNewItem">Create New Item</button>
</body>
</html>
3. Create a webpart page and add content editor webpart. Now add above file as reference.
4. When we click on 'Create New Item' then we should get below error message.
5. Reason for that error is while creating SharePoint column we given column name start with number. so in this case we should use "<d:EntityPropertyName>" instead of "<d:InternalName>".
now change the code as follow and test again.
//_x0036__x0020_Digit_x0020_Number : "123456" //Error
OData__x0036__x0020_Digit_x0020_Number: "123456" //Correct
6. Now Test again. Now Item should create in List.
No comments:
Post a Comment