I have a JSLink on my Webpart Custom List, and I need to add an item before render a view.
(function () {
var overrideCtx = {};
overrideCtx.Templates = {};
overrideCtx.Templates.Header = CustomHeader;
overrideCtx.Templates.Footer = CustomFooter;
overrideCtx.ListTemplateType = 100;
overrideCtx.Templates.Item = CustomItem;
overrideCtx.OnPreRender = CustomPreRender;
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();
function CustomPreRender(ctx){
CotacaoAppDataUpdate(ctx);
return true;
}
On CustomPreRender
I call this function:
function CotacaoAppInsert(ctx, data){
var listname = ctx.ListTitle;
if (typeof(data) == 'undefined' || data.length == 0) return false;
var itemType = GetItemTypeForListName(ctx.ListTitle);
for(i in data){
data[i]['Title'] = data[i]['Name'];
data[i]['From'] = data[i]['Name'].split('/')[0];
data[i]['To'] = data[i]['Name'].split('/')[1];
var date = moment(data[i]['Date']+' '+data[i]['Time'], 'M/D/YYYY hh:mma');
data[i]['Date'] = date.format('DD/MM/YYYY HH:mm');
delete data[i]['Name'];
delete data[i]['id'];
delete data[i]['Time'];
var item = $.extend({
"__metadata": { "type": itemType }
}, data[i]);
$.ajax({
url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listname + "')/items",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(item),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
}
}
ListTitle: Cotacao
Var itemType
: SP.Data.CotacaoListItem
My var item
on context:
Ask: "2.9834"
Bid: "2.9787"
Date: "23/04/2015 20:39"
From: "USD"
Rate: "2.9787"
Title: "USD/BRL"
To: "BRL"
__metadata: Object
__proto__: Object
Always I receive the error on console:
POST https://mydomain/sites/mysite/_api/web/lists/getbytitle('Cotacao')/items 400 (Bad Request)
I don't know what I'm doing wrong...
I have based this response and this post...
Edit
I've checked the responseText
:
{
"error": {
"code": "-1, Microsoft.SharePoint.Client.InvalidClientQueryException",
"message": {
"lang": "pt-BR",
"value": "Cannot convert a primitive value to the expected type 'Edm.DateTime'. See the inner exception for more details."
}
}
}
I don't know the correct date format from Sharepoint
type: "GET"
?Header
template, you can register it withoverrideCtx.OnPreRender
and it will execute before it gets to any header code.OnPreRender
, but I still receive error 400. @DylanCristy