Move/copy item from/to document library

Jan 5, 2010 at 6:36 AM

Hello !

I would like to know if does it possible to copy/move a document from a document library to another document library ?

Thanks in advance ! :-)

Coordinator
Jan 5, 2010 at 12:30 PM

Yes, it's possible using the CopyIntoItemsLocal operation of the Copy Web Service.  I've played around with this a bit and I was unable to figure out how to remove the link between the two documents.  Basically, it works like sending a copy to the other library.  If this meets your needs, then it works well.

M.

Jan 5, 2010 at 2:51 PM

Yes, my script works !! -)

Of course, thank to you ! :-)

<script language="javascript" type="text/javascript" src="/HRD/JQuery/jquery-1.3.2.min.js"></script>
<script language="javascript" type="text/javascript" src="/HRD/JQuery/spservices/jquery.SPServices-0.4.7.min.js"></script>
 
<script language="javascript" type="text/javascript" >
 
 
function PreSaveAction() {
 	$().SPServices({
		operation: "CopyIntoItemsLocal",
		async: false,
		SourceUrl: "http://philippe-ee2865/personal/administrator/Shared%20Documents/bradpitt_mini.jpg",
		DestinationUrls: ["http://philippe-ee2865/personal/administrator/Personal%20Documents/bradpitt.jpg"], 
		completefunc: function(xData, Status) {
			alert("Status=" + Status + " XML=" + xData.responseXML.xml);		
			}
	});
 
	return true;

}
</script>

So, I can copy a .jpg image to an other document library and rename it.

Coordinator
Jan 5, 2010 at 2:53 PM

Excellent! And another real example for the documentation.

M.

Jan 15, 2010 at 3:24 AM

thanks LeSanglier for the snipped code.
It give me an idea what js are needed to start using the lib and JQUERY escpecially if you are new to it.
Marc can  i use the same idea to copy an announcement items to another site on another server.
**note this items also do have attachments.

Can this be done using the lib?

Coordinator
Jan 15, 2010 at 3:23 PM

naijacoder:

Note that the Copy Web Service is there to move documents, not items.  I think you could probably accomplish what you want to do with a combination of other Web Services operations (GetListItems, UpdateListItems, etc.)

If you'd like some help, please start a new thread with the details.  There's also a new forum over at EndUserSharePoint.com called jQuery Library for SharePoint Web Services Solutions where you can discuss with a wider audience.  Whichever you prefer.

M.

Mar 16, 2011 at 8:59 PM

does anyone figure out how to remove the link between the two documents ???????

May 27, 2013 at 12:32 AM
I know this is old thread, but I needed the Copy Functionality without the Link as well, so here's the code (since there's no current examples). Note: You'll have to add your error-checking where appropriate. This is just to show others how to use.

You need to use the GetItem block first against your document to retrieve the document's Byte Stream; which then allows you to use the CopyIntoItems function which creates a new document at the destination. Note: you also get all the file Params from "GetItem" if that interest you. I just wanted the Byte Stream.
    var myStream = "";
    jQuery().SPServices({
        operation: "GetItem",
        async: false,
        Url: mOrgPath,
        completefunc: function(xData, Status) {
            myStream = jQuery(xData.responseXML).find("Stream").text(); //MSDN: base-64 representation of the retrieved document's binary data.
            } // end completefunc
    }); // end getItem
Now that we have the Byte stream, we can proceed with CopyIntoItems, which is very similar to CopyIntoItemsLocal with the exception that it allows multiple destinations, and requires the Byte steam.
    jQuery().SPServices({
        operation: "CopyIntoItems",
        async: false,
        SourceUrl: "http://myserver/mydoclib/myfile.pdf",
        DestinationUrls: ["http://myserver/mydestinationdoclib/myfile.pdf"],
        Stream: myStream, //whatever variable you used to store the base64 encoded binary data from the GetItem call
        completefunc: function(xData, Status) {
            alert("Status=" + Status + "\n" + xData.responseText);
            } // end completefunc
    }); // end CopyIntoItems
That's it I think, thanks for the awesome library!
Oct 28, 2014 at 8:37 PM
Edited Jan 5, 2015 at 6:18 PM
I'm chiming in on this old thread - I just had to resurrect some old code where we were using CopyIntoItemsLocal. I've built a tool where you can select a source and destination site, and copy over files, including metadata if need be.

I needed to update the code to come up with a way to unlink the Source item from its final destination. I was already copying the item, pulling its new ID, and then updating the recently copied file to include specific fields we are bringing over. That said, updating the MetaInfo field's "_CopySource" Property with a blank value seems to do the trick!

(EDIT: I originally wrote this from memory - I forgot the _CopySource field is actually a MetaInfo Property - see below)
$().SPServices({
     operation: "UpdateListItems",
     async: false,
     listName: Destination.LibName,
     webURL: Destination.Site,
     updates: "<Batch OnError='Continue' PreCalc='TRUE' >" +                                
     "<Method ID='1' Cmd='Update'>" +

    __"<Field Name='MetaInfo' Property='_CopySource'></Field>__" +__

     "<Field Name='ID'>" + docid + "</Field>" + 
     "<Field Name='Old_x0020_Path'>" + p + "</Field>" + 
     "<Field Name='Title'>" + ItemSet.Title + "</Field>" + 
     "</Method>" + 
     "</Batch>",
     completefunc: function (xData, Status) {
//DO STUFF

}
});
that's it! Note I am using the traditional (? old?) style of doing the update string. I do not know if we can use the ValuePairs method to update this, since it requires a fieldname and a parameter name - "MetaInfo" and "_CopySource".
Nov 4, 2015 at 3:38 PM
I'm trying to use j_a_q's method to copy items from a list attachment
var mOrgPath="https://onsite/managedpath/collection/site/Lists/listname/Attachments/id/filename.pdf"
 var myStream = "";
    jQuery().SPServices({
        operation: "GetItem",
        async: false,
        Url: mOrgPath,
        completefunc: function(xData, Status) {
            myStream = jQuery(xData.responseXML).find("Stream").text(); 
            }
    }); 
The url when pasted into the browser does give me the item I'm looking for and the request doesn't produce any errors, but the xData.responseText indicates that 0 items were retrieved and there is no Stream node in the xData.responseXML.

It does work if I try and get a document from a library, but not when trying to retrieve an attachment. Any ideas on that?