Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new to Javascript and currently working on a task where I need to copy files based on a custom column name "PID" which is a lookup column from One Document Library to the other.

I was able to get the below code to work which copies all the files

$scope.copyFiles=function()
{

var sourceLib = '/sites/Protocol/ProtocolDocument';
var destLib = '/sites/Protocol/FinalProtocolDocuments';


var context = new SP.ClientContext.get_current();
var web = context.get_web().get_lists();
var folderSrc = web.getFolderByServerRelativeUrl(sourceLib);
//var cq = "<Query><Where><Eq><FieldRef Name="ProtocolID" LookupId="TRUE"/><Value Type="Text">' + 466 + '</Value></Eq></Where></Query>"
context.load(folderSrc,'Files');
context.executeQueryAsync(
function() {

           console.log("Got the source folder right here!");
           var files = folderSrc.get_files();
           var e = files.getEnumerator();
           var dest = [];
           while (e.moveNext()) {
           var file = e.get_current();
           var destLibUrl = destLib + "/" + file.get_name();
           dest.push(destLibUrl); //delete this when we're happy we got the file paths right
           file.copyTo(destLibUrl, true);

           }

        console.log(dest); //delete this when we're happy we got the file paths right
        context.executeQueryAsync(function() { console.log("Files moved successfully!");}, function(sender, args) {console.log("error: ") + args.get_message()});
        }, 
    function(sender, args){console.log("Sorry, something messed up: " + args.get_message());}
    );   

   }


What I have tried:

I did some research online to get the Filenames based on a custom column value with no luck

Also tried to use CAML , however not sure how to use it in the code.

Would appreciate if anyone could help me get the filenames from a Document Library based on custom column name "PID" so that only selected/filtered files are moved to the destination Library
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900