1

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));
});
  • What do you mean by 1st row? 1st item in list or you have created custom table where you have bind the SharePoint list items and you want to update the 1st row of table? – Ganesh Sanap Jul 26 at 11:41
  • I want to update 1st row always i.e items in 1st row.It is the only row which is existing in a table. – user85213 Jul 26 at 11:43
  • This is custom table, right? You have fetched list items using REST API and those to custom table?? – Ganesh Sanap Jul 26 at 11:58
  • yes,this is a custom table/list.I have fetched items from another list using rest api and added/updated into this custom table/list. – user85213 Jul 26 at 12:07
  • 1
    Then you can use data.d.results[0].Id as ID of 1st item(from the results you are getting from your REST call). – Ganesh Sanap Jul 26 at 12:43
0

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>)"

  • Actually I am confused with this approach.List's ID column is auto incrementing.According to your approach,if I pass ID of first row the it's the same – user85213 Jul 26 at 12:16
  • Can you please add the code, how you have bind data to custom table? – Ganesh Sanap Jul 26 at 12:16
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.