I have successfully implemented a code that draws a table using CEWP SPO. Now, I am looking to enhance this functionality by adding a button to each row in the table. This button should be able to move the corresponding files from their current location to another document library within the same SPO site. The file names are retrieved using FileLeafRef.
To achieve this, I need to merge two functions. The data is currently being pulled using Ajax GET and the move command requires a POST type. I require assistance in merging these functions to achieve the desired outcome.
Current Code:
$(function(){
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('List_Name')/items?$top=10&$orderby=Created%20desc&$select=Reg,NBR,Date";
$.ajax({
url: requestUri,
type: "GET",
headers: {
"accept":"application/json; odata=verbose"
},
success: onSuccess,
});
function onSuccess(data) {
var objItems = data.d.results;
var tableContent = '<table id="Table"><tr><th>Reg</th><th>Num</th><th>Date</th></tr>';
for (var i = 0; i < objItems.length; i++) {
tableContent += '<tr>';
tableContent += '<td>' + objItems[i].Reg + '</td>';
tableContent += '<td>' + objItems[i].NBR + '</td>';
tableContent += '<td>' + moment(objItems[i].Date).format("DD-MM-YYYY") + '</td>';
tableContent += '</tr>';
}
$('#Grid').append(tableContent);
}
});
Move File Code:
function MoveFile(){
var oUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getfilebyserverrelativeurl('/Lib1/doc.docx')/moveto(newurl='/Lib2/doc.docx',flags=1)";
$.ajax({
url: oUrl,
type: "POST",
headers: {
"Accept": "application/json; odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function(data){
console.log(data);
},
error: function(data){
console.log(data);
}
});
}