I have a solution that adds a custom button to the ribbon in SharePoint 2010 and am using the javascript client object model behind the scenes to copy a file from Library A to Library B when the button is pressed. The copy works fine but instead of adding the file as a new version in Library B (if the file exists there already), the file in Library B is just overwritten and the version remains at v1.0.
I have:
- versioning turned on in Library B
- the javascript performs a check-out of the file in Library B and then performs the copy
Is there something I'm missing? Has anybody accomplished this before? Is this not possible via the Javascript CSOM in SharePoint 2010?
Code:
var _ctx;
var _sourceFile;
var _destinationlibUrl;
var _destinationFile;
// LibraryA is the name of the 'source' Document Library
// LibraryB is the name of the 'destination' Document Library
function CopyFile() {
// Get the current client context of the client-side object model (CSOM) runtime.
_ctx = new SP.ClientContext.get_current();
// 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('LibraryA');
_ctx.load(this.sourceList);
// Get the list items being selected.
var selectedDocuments = SP.ListOperation.Selection.getSelectedItems(_ctx);
this.currentItem = sourceList.getItemById(selectedDocuments[0].id);
_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() +
'/LibraryB/' +
_sourceFile.get_name();
// Get hold of the file in the destination library.
_destinationFile = web.getFileByServerRelativeUrl(_destinationlibUrl);
_ctx.load(_destinationFile);
_ctx.executeQueryAsync(Function.createDelegate(this, this.onLoadSucceeded),
Function.createDelegate(this, this.onQueryFailed));
}
}
// Delegate that is called when the destination file checkout completes successfully.
function onLoadSucceeded(sender, args) {
if (_destinationFile.get_exists() == true) {
_destinationFile.checkOut();
}
_sourceFile.copyTo(_destinationlibUrl, true);
notifyId = SP.UI.Notify.addNotification('Copying 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);
//if (_destinationFile.get_exists() == true) {
//_destinationFile.checkIn('Document Check-In',SP.CheckinType.majorCheckIn);
//}
// Redirect to Library B when complete
window.location = web.get_serverRelativeUrl() + '/LibraryB/';
},
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 server operation is completed with errors.
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}