I have studied and used the following post to attempt to move a document: moveTo and moving documents from one library to another
This works perfectly when moving documents to different libraries in the same web or even folders in the same library but I am trying to move a document to a different library in the parentweb.
- Is this possible?
- How can I do it?
So far I have changed the code to this which is returning error 0x002 "Illegal Argument Exception":
//current web is "/web/sub/";
var context = SP.ClientContext.get_current();
var site = context.get_site();
var dst = "/web/lib/fold/";
var dstweburl = dst;
dst += "myfile.txt";
var destweb = site.openWeb(dstweburl.substring(0,dstweburl.substring(0, dstweburl.length-1).lastIndexOf("/")+1));
var destfold = destweb.getFolderByServerRelativeUrl(dstweburl);
context.load(destfold);
var web = context.get_web();
context.load(web);
var currentLib = web.get_lists().getById(unescape(ListGUID));
var currentItem = currentLib.getItemById(ItemID);
context.load(currentItem);
var File = currentItem.get_file();
context.load(File);
//Excecuting executeQueryAsync to get the loaded values
context.executeQueryAsync(
function(sender, args){
if(File!= null){
if(moveFlag){
File.moveTo(dst, SP.MoveOperations.overwrite);
}
else{
File.copyTo(dst,true);
}
//Excecuting executeQueryAsync to copy the file
context.executeQueryAsync(
function (sender, args) {
$("<div>Item moved/copied successfully.</div>").appendTo("#response");
},
function (sender, args) {
$("<div>Error moving/copying item. 0x002</div>").appendTo("#response");
}
);
}
else{
$("<div>Error moving/copying item. 0x001</div>").appendTo("#response");
}
},
function(sender, args){
$("<div>Error moving/copying item. 0x000</div>").appendTo("#response");
}
);
SP.File.moveTo
function only supports moving documents within the same site. It will not allow you to move files to other sites.SP.File.moveTo