How to get item id of newly copied file from one document library to another using SharePoint 2013 JavaScript Client Object Model

I want to copy multiple files from one document library to another with metadata and column values associated from the copied file to the destination file. I am using sharepoint 2013 javascript client object model. I referred to the code at this http://sharepoint.stackexchange.com/a/100327link, but I am unable to get item IDs of newly copied files.

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">

var _ctx;
var _sourceFile;
var _destinationlibUrl;
var _destinationFile;
var siteUrl = '/sites/intestsite';
var targetListItem;
var itemId=12;

    $(document).ready(function () {


    $("#btnCopy").on("click", function(){

    CopyFile() ;
    })
});

function CopyFile() {
    _ctx = new SP.ClientContext(siteUrl);

    // Get the Web site that is associated with the client context.
    this.web = _ctx.get_web();
    _ctx.load(this.web);

    // Returns the list with the specified title from the collection.
    this.sourceList = this.web.get_lists().getByTitle('Pages');
    _ctx.load(this.sourceList);

    // Get the list items being selected.
    //var selectedDocuments = SP.ListOperation.Selection.getSelectedItems(_ctx);
    //this.currentItem = sourceList.getItemById(selectedDocuments[0].id);

    this.currentItem = sourceList.getItemById(itemId);
    _ctx.load(this.currentItem);

    // Get the file that is represented by the item from a document library.
    _sourceFile = this.currentItem.get_file();
    _ctx.load(_sourceFile);

    _ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),
                           Function.createDelegate(this, this.onQueryFailed));
}

// Delegate that is called when the query completes successfully.
function onQuerySucceeded(sender, args) {


    if (_sourceFile != null) {
        _destinationlibUrl = web.get_serverRelativeUrl() +'/PageArchive/' + _sourceFile.get_name();
         alert('Now moving to: ' + _destinationlibUrl);

         //_sourceFile.moveTo(_destinationlibUrl, 1);
        _sourceFile.copyTo(_destinationlibUrl, 1);

         notifyId = SP.UI.Notify.addNotification('Moving file ' + _sourceFile.get_serverRelativeUrl() + ' to ' + _destinationlibUrl, true);


        _ctx.executeQueryAsync(
        function (sender, args) {
            SP.UI.Notify.removeNotification(notifyId);
            SP.UI.Notify.addNotification('File copied successfully', false);

            window.location = web.get_serverRelativeUrl();
        },
        function (sender, args) {
            SP.UI.Notify.addNotification('Error copying file', false);
            SP.UI.Notify.removeNotification(notifyId);
            alert('Error occured: ' + args.get_message());
        }
    );

    }

}

// Delegate that is called when the destination file checkout completes successfully.


// Delegate that is called when server operation is completed with errors.
function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>

May 31st, 2015 5:11pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics