When I run the code in console the item is getting created with empty columns, but when I run same code with input values it is showing as request failed. Following is my code..Check it and help me...Thanks in advance
$(document).ready(function() {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Ideas')/items('69')?$select=IdeaId,Title",
method: "GET",
headers: { "Accept": "application/json; odata=verbose", "Content-Type": "application/json;odata=verbose", },
success: function (response) {
if (response.d != undefined) {
var ResultDiv = response.d;
$("#ideaId").val(ResultDiv.IdeaId);
$("#ideaDescription").val(ResultDiv.Title);
}
},
/*error: function (response) {
$("#ResultDiv").empty().text(responseJSON.error);
}*/
});
//Add an item to list
createListItem();
//Update an item
updateListItem();
});
function createListItem() {
use strict;
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle("RiskRatings");
var itemCreateInfo = new SP.ListItemCreationInformation();
var oListItem = oList.addItem(itemCreateInfo);
var listItemRole = document.getElementById("txtraterrole").value;
alert(listItemRole);
var listItemName = document.getElementById("txtratername").value;
alert(listItemName);
var listItemRating = document.getElementById("txtrating").value;
alert(listItemRating);
var listItemComments = document.getElementById("txtriskcomments").value;
alert(listItemComments);
oListItem.set_item('RaterRole', listItemRole);
oListItem.set_item('RaterName', listItemName );
oListItem.set_item('Rating', listItemRating);
oListItem.set_item('RiskComments', listItemComments);
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
alert('Item created: ' + oListItem.get_id());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
function clearFields(){
document.getElementById("txtraterrole").value="";
document.getElementById("txtratername").value="";
document.getElementById("txtrating").value="";
document.getElementById("txtriskcomments").value="";
}
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Yahiya IqbalPosted Jan 19, 2017, 7:53 AM
I am not able to get what you are trying to in your code. but here is code to create Item in list. Please Change column names and list name in your code.