0

I am making a POST request to Sharepoint List and in that I have to add items more than 150 which takes my like 45-50 mins to add all the Line details. And once I have added I click on submit and then I got to network tab i see error. Below is my code for adding those 150 items.

 if ($("#tbodyRAssetMaterial")[0].children.length > 0) {
            for (i = 0; i < $("#tbodyRAssetMaterial")[0].children.length; i++) {
                RSrNo = $("#lblRSrNo" + itemCount).text();
                RAssetNo = $("#txtRAssetNo" + itemCount).val();
                RDescriptionOfItems = $("#txtRDescriptionOfItems" + itemCount).val();
                RQuantity = $("#txtRQuantity" + itemCount).val();
                RUnit = $("#txtRUnit" + itemCount).val();
                RDateOfReturn = moment($("#dtRDateOfReturn" + itemCount).val()).format(
                    "YYYY-MM-DD"
                );
                RRemarks = $("#txtRRemarks" + itemCount).val();
                var quantity = 0;
                sumOfTotalQuantity = parseFloat(sumOfTotalQuantity) + parseFloat(RQuantity);
                $.ajax({
                    url: _spPageContextInfo.webAbsoluteUrl +
                        "/_api/web/lists/GetByTitle('ASMGP')/items",
                    type: "POST",
                    data: JSON.stringify({
                        __metadata: {
                            type: GetItemTypeForListName("ASMGP"),
                        },
                        Title: RSrNo,
                        AssetNo: RAssetNo,
                        DescriptionOfItems: RDescriptionOfItems,
                        Quantity: RQuantity,
                        Unit: RUnit,
                        ExpectedDateOfReturn: RDateOfReturn,
                        GatePassMasterID: itemId.toString(),
                        GatePassIdId: itemId,
                        Remarks: RRemarks,
                    }),
                    headers: {
                        Accept: "application/json;odata=verbose",
                        "Content-Type": "application/json;odata=verbose",
                        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                        "X-HTTP-Method": "POST",
                    },
                    success: function(data, status, xhr) {
                    alert("Your Gatepass Created Successfully. Serial No.: " + NewSrNo);
                    },
                    error: function(xhr, status, error) {
                        //$(".page-loader").addClass("d-none");
                    },
                });
               itemCount++;
            }
            UpdateSumOfTotalQuantity(itemId, sumOfTotalQuantity, NewSrNo);
       }

    }

the security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again.

As per google's suggestion i have also added "X-RequestDigest": $("#__REQUESTDIGEST").val(), in Headers section, still Im unable to to do the POST request. Im trying on SharePoint Online. Please help, any suggestion will be appreciated.!!!

1 Answer 1

0

When add items to sharepoint by restapi. metadata should be like following

  __metadata:
            {
                // Format of the "type" is: SP.Data.<<ListName>>ListItem
                type: "SP.Data.ASMGPListItem"
            }

headers should be like following

headers:
        {
            // Accept header: Specifies the format for response data from the server.
            "Accept": "application/json;odata=verbose",
            //Content-Type header: Specifies the format of the data that the client is sending to the server
            "Content-Type": "application/json;odata=verbose",
            // X-RequestDigest header: When you send a POST request, it must include the form digest value in X-RequestDigest header
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        }
1
  • I am checking to see if the problem has been resolved. Commented Aug 4, 2022 at 9:07

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.