1

I am new to Javascript and currently working on a task where I need to copy files based on a custom column name "PID" 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());}
    );   

   }

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.

0

Use the camel query something like below(which you have already and commented), little bit I have modified for the custom column condition :

var cq = "<Query><Where><Eq><FieldRef Name="yourcustomcolumn" /><Value Type="Text">PID</Value></Eq></Where></Query>"

For exact query syntax and recommendation to use camel query in the code - use camel query builder tool where run the select query to get the item with condition "customcolumnvalue - eq " PID" and finally use that query in the code.

Note:

  • Writing camel query directly in the code is not recommended, chances are getting syntantical errors are more. So we should test the query first in the camel query builder tool, and if we get successful results then only copy the query to the actual code.

How to use camel query in Javascript - Example

SP.SOD.executeOrDelayUntilScriptLoaded(showListItems,"core.js");

function showListItems(){
    var clientContext = new SP.ClientContext();
    var list = clientContext.get_web().get_lists().getByTitle("List Title");
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(
        "<View><Query>" +
            "<Where>" +
                "<Eq><FieldRef Name=\"Title\"/><Value Type=\"Text\">Value</Value></Eq>" + 
            "</Where>" +
            "<OrderBy><FieldRef Name=\"Modified\" Ascending=\"FALSE\"/></OrderBy>" +
        "</Query>"+
        //"<RowLimit>5000</RowLimit>" +
        "</View>");
    var items = list.getItems(camlQuery);
    clientContext.load(items);
    clientContext.executeQueryAsync(function(){
        var itemArray = [];
        var itemEnumerator = items.getEnumerator();
        while(itemEnumerator.moveNext()){
            var item = itemEnumerator.get_current();
            var id = item.get_item("ID");
            var title = item.get_item("Title");
            itemArray.push(id + ": " + title);
        }
        alert("ID: Title\n"+itemArray.join("\n"));
    },function(sender,args){alert(args.get_message());});
}

Reference:

Get List Items by CAML Query

REST API query to get the specific file in SharePoint:

https://[site]/web/Lists/GetByTitle('[library_name]')/Items?$filter=substringof('[TEXT_TO_SEARCH]',Title) or substringof('[TEXT_TO_SEARCH]',FileLeafRef)&$expand=File,FieldValuesAsText

Example:

https://[site]/web/Lists/GetByTitle('[library_name]')/Items?$select=ID,Title&$customColumnVal='PID'

Source:

Find a list of Files using Sharepoint REST API

Here is a one more dynamic custom camel query builder tool :

https://archive.codeplex.com/?p=camljs

| improve this answer | |
  • Hi Habibur, Thank you for the response , I have already written the caml query in my code , however I am not sure how to use it in the code to get the files I need based on PID , also is there any other technique of doing it? – Vivek Murli Apr 18 at 11:40
  • Refer to the below example, how to use camel query in javascript :sharepoint.stackexchange.com/questions/95189/… – Habibur Rahaman Apr 18 at 11:53
  • And also you can go with powershell coding which is more robust - develop a PowerShell script to move the files, and schedule that script in Windows task scheduler which will run periodically like timer job. This solution is more stable and robust than the javascript. – Habibur Rahaman Apr 18 at 11:55
  • Refer to this URL on how to use camel query in the Javascript code: riptutorial.com/sharepoint/example/7817/… – Habibur Rahaman Apr 18 at 12:05
  • Thank you again for the response Habibur , I would surely check the articles .. However is there any other way apart from caml query to get the work done? – Vivek Murli Apr 18 at 12:17

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.