Question

I am using REST API to update in custom list of SharePoint Server. It is a dashboard and displays result as per logged in user. Is it possible to update 1st row always using REST API irrespective of ID of row? Rest API is updating table but I want to update 1st row always.

CODE:

function UpdatePendingListItem(value)  
{  

    $.ajax  
    ({  
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('<LIST_NAME>')/items(<ITEM_ID>)", // list item ID  
        type: "POST",  
        data: JSON.stringify  
        ({  
            __metadata:  
            {  
                type: "SP.Data.<LIST_NAME>ListItem"  
            },  
           COLUMN_NAME1:value,
COLUMN_NAME2: loginName 
        }),  
        headers:  
        {  
            "Accept": "application/json;odata=verbose",  
            "Content-Type": "application/json;odata=verbose",  
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
            "IF-MATCH": "*",  
            "X-HTTP-Method": "MERGE"  
        },  
       async: false,  
        success: function(data, status, xhr)  
        {  

        },  
        error: function(xhr, status, error)  
        {  

        }  
    });

}

**code to call function and update:**
getListItemsForView(_spPageContextInfo.webAbsoluteUrl,'<list_name>','<View_name>')
.done(function(data)
{
     var items = data.d.results;
    console.log("Waiting For Approval=" +data.d.results.length);
     //for(var i = 0; i < items.length;i++) {
       //  console.log(items[i].Title);
     //}    

 if(DashboardItemCount==0)
{
      CreatePendingListItem(data.d.results.length);
}
else
{
UpdatePendingListItem(data.d.results.length);
}
})
.fail(
function(error){
    console.log(JSON.stringify(error));
});
Était-ce utile?

La solution 2

Adding

var rowId=$(".ms-listviewtable>tbody>tr").attr("id")

in success of rest api call and passing rowid as id of a row,solved my issue.

As I display different list per user,I have to use this approach.

I f your list is same for every user then you can go for data.d.results[0].Id approach also.

Autres conseils

You can follow steps like below:

  1. Fetch the list items from SharePoint list.
  2. Bind the list items to your custom table along with the ID of list item (If you don't want to show the ID to user then you can hide that column from table using CSS).
  3. Whenever you are calling update item function, you can pass the ID of first row of your table instead of <ITEM_ID>.

    _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('<LIST_NAME>')/items(<ITEM_ID>)"

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top