3

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.

  1. Is this possible?
  2. 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. – Vardhaman Deshpande Feb 17 '14 at 20:18
  • @VardhamanDeshpande when you say in the same "site" are you refering to site collection or web? – Realistic Feb 17 '14 at 20:37
  • I meant web. Sorry for the confusion. You cannot move a document to another web within the same SiteCollection with SP.File.moveTo – Vardhaman Deshpande Feb 17 '14 at 21:16
  • Take a look at Mikael Svenson's article. He has explained how to move/copy file between different sites using REST. Though the code he has uses AppContext, you should be able to do it without App. techmikael.blogspot.com/2013/07/… – Aveenav Feb 17 '14 at 21:50
  • @Aveenav his post is regarding SP 2013. I am on SP 2010. – Realistic Feb 20 '14 at 13:36
1

I have instead used the SPServices JS library. Code for moving with the metadata is here: Copy document from one site to another

| improve this answer | |

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.