简体   繁体   中英

How can I succesfully update multiple items on a sharepoint list using AJAX?

Apologies as I have already asked a v. similar question.

I am trying to update a sp list with the following javascript/ajax. It succeeds until it gets to the ajax function, which is where it fails. It goes down the error route and in "alert(err.Message)" it posts "undefined"

Any help appreciated.

  <script type="text/javascript">
    function updateMultipleListItems(){
        var listName="Address Validation";
        //CustomerNumber.val("16");
        var CustomerNumber="CustNum";
        $.ajax({
url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items?$select=ID&$filter=Cust_x0020_Number eq 17",

            type: "GET",       
            headers: {
                "Accept": "application/json;odata=verbose",
            },
            success: function (data) {       
                for (var i = 0; i < data.d.results.length; i++) {  
                    var item = data.d.results[i];  
                    alert("1");
                    var itemType = GetItemTypeForListName(listName);
                    alert("2");
                    var ItemId = item.ID;
                    alert("3");
                    var item = {
                    "__metadata": { 
                    "type": 'SP.Data.Address%20ValidationListItem'
                    }, 
                    "assign": "testinput" 
                    };      
                    alert("4");             
                    $.ajax({
                        url:_spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items('" + ItemId + "')",
                        type: "POST",
                        contentType: "application/json;odata=verbose",
                        data: JSON.stringify(item),
                        headers: {
                            "Accept": "application/json;odata=verbose",
                            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                            "X-HTTP-Method": "MERGE",
                            "If-Match": "*"
                        },
                        success: function (data) {
                            console.log('Update Success');
                            alert("Success");

                        },
                    error: function(xhr, status, error) {
                    var err = eval("(" + xhr.responseText + ")");
                    alert(err.Message);
    }
                    });

                } 
            },
            error: function (data) {
                alert("Error");
            }
        });
    }
    function GetItemTypeForListName(name) {
        return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
    }  
    </script>

What I'm trying to do:

I am trying to update all records in a list where the cust_number (a column in the list) field is 17 so that assign (another column) = "testinput".

eg:
Cust Number| Assign
17 | testinput
1 |
17 | testinput
显示错误消息,如建议

So I found out that my metadata type was incorrect. I went to the following url:

sitename/apps/reserve/_api/lists/getbytitle('Address%20Validation')?$select=ListItemEntityTypeFullName    

From here I found the correct ListItem type.

Thanks,

Kieran

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM