in JavaScript Technology

Move Files Between Document Libraries Using Rest API and JavaScript

Question

Code below works to draw a table using CEWP SPO. Need to add a button on every row in the same table that would move corresponding files from it’s current location to another document library in the same SPO site. File names are retrieved using FileLeafRef.

Read also about Double Arraylist Java

The data is pulled using the Ajax GET type and move command would use POST type. Kindly assist to merge both functions to get the desired.

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" style="width:100%"><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);
            }
        });
    }

Answer

Merged code is the following:

<script>


    $(function () {
        var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Documents')/items?$top=10&$orderby=Created desc&$select=*,FileLeafRef";
        $.ajax({
            url: requestUri,
            type: "GET",
            headers: {
                "accept": "application/json; odata=verbose"
            },
            success: onSuccess,
        });
        function onSuccess(data) {
            var objItems = data.d.results;
            console.log(objItems)
            var tableContent = '<table id="Table" style="width:100%"><tr><th>ID</th><th>Title</th><th>Date</th></tr>';

            for (var i = 0; i < objItems.length; i++) {
                if (objItems[i].FileSystemObjectType != 1) {
                    var fileName=encodeURI(objItems[i].FileLeafRef);
                    var button = "<input type='button' value='MoveFile' onclick= MoveFile('"+encodeURI(objItems[i].FileLeafRef) +"')>"
                    tableContent += '<tr>';
                    tableContent += '<td>' + objItems[i].ID + '</td>';
                    tableContent += '<td>' + objItems[i].FileLeafRef + '</td>';
                    tableContent += '<td>' + moment(objItems[i].Created).format("DD-MM-YYYY") + '</td>';
                    tableContent += '<td>' + button + '</td>';
                    tableContent += '</tr>';

                }
            }
            $('#Grid').append(tableContent);
        }
    });

    function MoveFile(FileLeafRef) {
        var sourceFile = "/sites/NewProvision/Shared Documents/" + FileLeafRef;
        var targetFile = '/sites/NewProvision/doc1/' + FileLeafRef;
        var oUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFileByServerRelativePath(decodedurl='" + sourceFile + "')/moveto(newurl='" + targetFile + "',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);
            }
        });
    }
</script>
<div id="Grid">

In MoveFile function, pass a “FileLeafRef” as a parameter for the specific file name and define sourceFile/targetFile parameter for set file URL dynamically.

enter image description here

Note! Remove all the space in file name.

Click here to view original web page at stackoverflow.com