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;



function CustomPreRender(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]);

         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) {
         error: function (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...


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

  • If you are trying to "get" list by title, shouldn't your ajax request be type: "GET"? Apr 23, 2015 at 20:41
  • I'm trying add an item on my list @DylanCristy...
    – Carlos
    Apr 23, 2015 at 20:45
  • I see. Also, if you are trying to add the item before rendering the view, instead of calling that function from the Header template, you can register it with overrideCtx.OnPreRender and it will execute before it gets to any header code. Apr 23, 2015 at 20:53
  • I already registered the function on OnPreRender, but I still receive error 400. @DylanCristy
    – Carlos
    Apr 23, 2015 at 21:00
  • Hi @DylanCristy, I've edited the question... Can you help me?
    – Carlos
    Apr 23, 2015 at 21:21

1 Answer 1


I've solved error changing date format...


  var date = moment(data[i]['Date']+' '+data[i]['Time'], 'M/D/YYYY hh:mma');
  data[i]['Date'] = date.format('DD/MM/YYYY HH:mm');

To this:

  var date = moment(data[i]['Date']+' '+data[i]['Time'], 'M/D/YYYY hh:mma');
  data[i]['Date'] = date.format('YYYY-MM-DDTHH:mmZ');
  • This format is called ISO8601 and I believe the Time part is optional so you could also just give YYYY-MM-DD to SP. Apr 24, 2015 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.