Hi All,
I am trying to add a new task to a Tasks list called CATasks. The list was created via Visual Studio “Add > New Item > List” process and not by custom code. The code I am using to add a task is shown below.
What I have noticed is if I use my code to add a Task to a Task List created by Visual studio “Add > New Item > List” process, I get an error. Error message when adding an item:
Call failed. Error: An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartObject' node was expected.
But if I create the same list using custom code I don't get an error.
Am I missing something in my custom code or is this a bug?
I hope you can help.
CEStar
***********
App.js Code:
function createItem() {
var call = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/?$select=Title,CurrentUser/Id&$expand=CurrentUser/Id",
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});
call.done(function (data, textStatus, jqXHR) {
var userId = data.d.CurrentUser.Id;
addItem(userId);
});
call.fail(function (jqXHR, textStatus, errorThrown) {
failHandler(jqXHR, textStatus, errorThrown);
});
function addItem(userId) {
var due =new Date();
due.setDate(due.getDate() + 7);
var call = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('CATasks')/Items",
type: "POST",
data: JSON.stringify({
"__metadata": { type:"SP.Data.CATasksListItem" },
Title: "Sample Task",
AssignedToId: userId,
DueDate: due
}),
headers: {
Accept: "application/json;odata=verbose",
"Content-Type":"application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
}
});
call.done(function (data, textStatus, jqXHR) {
var div = jQuery("#message");
div.text("Item added");
});
call.fail(function (jqXHR, textStatus, errorThrown) {
failHandler(jqXHR, textStatus, errorThrown);
});
}
function failHandler(jqXHR, textStatus, errorThrown) {
var response = JSON.parse(jqXHR.responseText);
var message = response ? response.error.message.value : textStatus;
alert("Call failed. Error: "+ message);
}
}
*******************
Create List Custom Code:
function createList() {
var call = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists",
type: "POST",
data: JSON.stringify({
"__metadata": { type:"SP.List" },
BaseTemplate: SP.ListTemplateType.tasks,
Title: "CATasks"
}),
headers: {
Accept: "application/json;odata=verbose",
"Content-Type":"application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
}
});
call.done(function (data, textStatus, jqXHR) {
var message = jQuery("#message");
message.text("List added");
});
call.fail(function (jqXHR, textStatus, errorThrown) {
var response = JSON.parse(jqXHR.responseText);
var message = response ? response.error.message.value : textStatus;
alert("Call failed. Error: "+ message);
});
}
*******************