I'm in a bit of a quandry as I'm just learning how to do development in SharePoint 2016 / SharePoint Online and using Javascript to perform REST calls. I have a form that is using REST to successfully pull data from a SharePoint list using the following:
function getListItems(webURL, listName, successFunction, successParams, failFunction) { // REST Call $.ajax({ url: webURL + "/_api/web/lists/GetByTitle('" + listName + "')/items?$top=1000", type: "GET", headers: { "ACCEPT": "application/json;odata=verbose" }, success: function(data) { successFunction(data, successParams); }, error: function(error) { failFunction(error); } }); };
I also have the following to submit data to a single SharePoint list, which isn't quite working (it submits to the list, but calls the fail function). Could someone help me to understand why it is failing?
// Get List Item Type metadata function GetItemTypeForListName(name) { return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem"; } function submitListItems(webURL, listName, jsonObj, successFunction, failFunction) { // REST Call to create new list item var jsonMeta = {'__metadata': {'type': GetItemTypeForListName(listName)}}; // Merge the two JSON lists to include SP metadata information jsonObj = $.extend(jsonMeta, jsonObj); $.ajax({ url: webURL + "/_api/web/lists/GetByTitle('" + listName + "')/items", type: 'POST', contentType: 'application/json;odata=verbose', data: JSON.stringify(jsonObj), headers: { 'ACCEPT': 'application/json;odata=verbose', 'X-RequestDigest': $('#__REQUESTDIGEST').val() }, success: function(data) { successFunction(data, jsonObj); }, error: function (error) { console.log(JSON.stringify(error)); failFunction(error); } }); };
The form I am working with is best designed to submit data to two separate lists, with the lists having a 1:Many relationship on a common field primary key that is created during the submit process for list1, and the lists would join on list1.pk_id_1 = list2.sk_id1 (if SharePoint could do list joins). Thus my two lists are set up as:
list1: PK_ID_1 item1 item2 item3 item4 ... PK_ID_2 item1 item2 item3 item4 ... PK_ID_3 item1 item2 item3 item4 ... list2: PK_1 SK_ID_1 item1a item2a item3a item4a PK_2 SK_ID_1 item1b item2b item3b item4b ... PK_3 SK_ID_1 item1c item2c item3c item4c ... PK_4 SK_ID_2 item1a item2a item3a item4a ... PK_5 SK_ID_2 item1b item2b item3b item4b ...
Since the form is tied to a single submit button, I need to somehow set up or modify the above submitListItems function to submit two separate JSON objects to two separate lists, at the same time, and then report on the success or fail status. If either fail, it needs to roll back the changes on the one that did succeed and report back to the user to resubmit.
How can I best go about this? I had started reading into batch processing at this link, but I need to modify the above structure to submit two separate JSON objects to their respective lists.
Additionally, the JSON object I am passing the function works fine for the items going into list1, being a simple:
var jsonObj = { key1: value, key2: value, key3: value, key4: value };