Question

I'm trying to insert an item in an external list (a SQL server table linked to SP through BCS) via javascript...i thought it was simple but...

this is the function for insert

this.insertListItem = function (nameList, internalNameList) {
    var baseURL = _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('" + nameList + "')/items";
    var itemPayload = {
    '__metadata': {'type': internalNameList},
        "field1": 1,
        "field2": 2,
        "field3": 3
    };

    $.ajax({       
    url: baseURL ,   
    type: "POST",   
    data: JSON.stringify(itemPayload),
    contentType: "application/json;odata=verbose",
    headers: { 
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest" : $("#__REQUESTDIGEST").val()
    },
    success: function (data) {
        console.log(data);
    },
    error: function (data) {
        console.log(data);
    }
    });            
}

when I launch the function the server returns a 201 status and this is the response

{
   "d":{
      "__metadata":{
         "id":"Web/Lists(guid'961bf4da-1748-49af-9926-78a3167978a8')/GetItemByStringId(null)",
         "uri":"http://cs001000365508/sites/HR/Cruscotto/_api/Web/Lists(guid'961bf4da-1748-49af-9926-78a3167978a8')/GetItemByStringId(null)",
         "type":"SP.Data.Conn_x005f_cr_x005f_riserve_x005f_lstListItem"
      },
      "FirstUniqueAncestorSecurableObject":{
         "__deferred":{
            "uri":"http://cs001000365508/sites/HR/Cruscotto/_api/Web/Lists(guid'961bf4da-1748-49af-9926-78a3167978a8')/GetItemByStringId(null)/FirstUniqueAncestorSecurableObject"
         }
      },
      "RoleAssignments":{
         "__deferred":{
            "uri":"http://cs001000365508/sites/HR/Cruscotto/_api/Web/Lists(guid'961bf4da-1748-49af-9926-78a3167978a8')/GetItemByStringId(null)/RoleAssignments"
         }
      },
      "AttachmentFiles":{
         "__deferred":{
            "uri":"http://cs001000365508/sites/HR/Cruscotto/_api/Web/Lists(guid'961bf4da-1748-49af-9926-78a3167978a8')/GetItemByStringId(null)/AttachmentFiles"
         }
      },
      "ContentType":{
         "__deferred":{
            "uri":"http://cs001000365508/sites/HR/Cruscotto/_api/Web/Lists(guid'961bf4da-1748-49af-9926-78a3167978a8')/GetItemByStringId(null)/ContentType"
         }
      },
      "FieldValuesAsHtml":{
         "__deferred":{
            "uri":"http://cs001000365508/sites/HR/Cruscotto/_api/Web/Lists(guid'961bf4da-1748-49af-9926-78a3167978a8')/GetItemByStringId(null)/FieldValuesAsHtml"
         }
      },
      "FieldValuesAsText":{
         "__deferred":{
            "uri":"http://cs001000365508/sites/HR/Cruscotto/_api/Web/Lists(guid'961bf4da-1748-49af-9926-78a3167978a8')/GetItemByStringId(null)/FieldValuesAsText"
         }
      },
      "FieldValuesForEdit":{
         "__deferred":{
            "uri":"http://cs001000365508/sites/HR/Cruscotto/_api/Web/Lists(guid'961bf4da-1748-49af-9926-78a3167978a8')/GetItemByStringId(null)/FieldValuesForEdit"
         }
      },
      "File":{
         "__deferred":{
            "uri":"http://cs001000365508/sites/HR/Cruscotto/_api/Web/Lists(guid'961bf4da-1748-49af-9926-78a3167978a8')/GetItemByStringId(null)/File"
         }
      },
      "Folder":{
         "__deferred":{
            "uri":"http://cs001000365508/sites/HR/Cruscotto/_api/Web/Lists(guid'961bf4da-1748-49af-9926-78a3167978a8')/GetItemByStringId(null)/Folder"
         }
      },
      "ParentList":{
         "__deferred":{
            "uri":"http://cs001000365508/sites/HR/Cruscotto/_api/Web/Lists(guid'961bf4da-1748-49af-9926-78a3167978a8')/GetItemByStringId(null)/ParentList"
         }
      },
      "FileSystemObjectType":0,
      "Id":0,
      "BdcIdentity":null,
      "field1":null,
      "field2":null,
      "field3":null
   }
}

Does anyone have an idea?

Était-ce utile?

La solution

ok...first version of the script (with $.ajax) is the regular way to execute CRUD operations on items in lists (external and internal)..unfortunately it works only for internal list (by my experience)...so i've searched (for days) another way to execute insert statement (update and delete works with $.ajax) and I found a solution in a book (https://www.amazon.com/Microsoft-SharePoint-Development-Developer-Reference/dp/0735674981) which suggests to use executeQueryAsync (https://msdn.microsoft.com/en-us/library/office/dn168907.aspx), a simple method used to execute pending request on server...so i get context and a reference to external list in which want to insert item, i set up item with field values (passed by an array) and then, because of executeQueryAsync doesn't support promise i've implemented it by myself (so I can usen .then syntax from the caller)

I hope i'm clear

this.insertListItem = function (nameList, values) {
    var ctx = new SP.ClientContext.get_current();
    var list = ctx.get_web().get_lists().getByTitle(nameList);
    ctx.load(list);
    var listItemCreationInfo = new SP.ListItemCreationInformation();
    var newItem = list.addItem(listItemCreationInfo);

    for(var prop in values){             
    newItem.set_item([prop], values[prop]);
    }

    newItem.update();

    var deferred = $.Deferred();
    ctx.executeQueryAsync(function(sender, args) {
    deferred.resolve(sender, args);
    }, function(sender, args) {
    deferred.reject(sender, args);
    });

    return deferred.promise();
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top