Ebse

Hello together,

i want to copy a document from one Document Library to another at the same Sharepoint site.

Can someone please post me a code sample for this I tried a view things from the msdn, without success.

Thanks in advance.


Re: SharePoint - Development and Programming Copy Document between two Document Libraries

James Yarnell

Use the CopyTo or MoveTo methods on the SPFile object. The SDK has a pretty good example.





Re: SharePoint - Development and Programming Copy Document between two Document Libraries

Eric Shupps [MVP]

You can also use the SharePoint Web Services like this (allows you to copy items between site collections):

Instantiate an instance of the Copy web service and perform the copy operation. Use the CopyIntoItemsLocal method of the web service. This method takes two parameters, the source file and the destination file. It also has an output parameter for the results of the method. The destination parameter is an array of strings to accomadate multiple detinations in one operation. The results parameter is also an array to accomadate the results for each destination.

wfCopy.Copy myCopy = new Sample.wfCopy.Copy();

myCopy.Credentials = System.Net.CredentialCache.DefaultCredentials;

wfCopy.CopyResult result = new Sample.wfCopy.CopyResult();

wfCopy.CopyResult[] results = { result };

uint myUint = myCopy.CopyIntoItemsLocal(libDocSrc, libDocDest, out results);





Re: SharePoint - Development and Programming Copy Document between two Document Libraries

Ebse

Thanks for your reply.

I tried it now this way:

Code Block

Public Sub copyspecs()
Dim site As New SPSite("http://mydomain.de")
Dim web As SPWeb = site.AllWebs("/")
Dim files As SPFileCollection = web.GetFolder("http://mydomain.de/Test").Files

Dim i As Integer

For i = 0 To files.Count - 1
files(i).CopyTo("http://mydomain.de/Temp/" & files(i).Name)
Next i
End Sub



But I get this error message:

Code Block

The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.



Any idea

Thanks in advance.




Re: SharePoint - Development and Programming Copy Document between two Document Libraries

Ebse


I have the solution.

Code Block

Public Sub copyspecs()
Dim site As New SPSite("http://difa.itb-services.de")
Dim web As SPWeb = site.AllWebs("/")
Dim files As SPFileCollection = web.GetFolder("http://difa.itb-services.de/Test/").Files

web.AllowUnsafeUpdates = True

Dim i As Integer

For i = 0 To files.Count - 1
files(i).CopyTo("http://difa.itb-services.de/Temp/" & files(i).Name)
Next i
End Sub


The "AllowUnsafeUpdates" must be set on true.

But now another, question: Is it possible to copy also the meta data and select some of the document by a meta data field like "Release = 1.0"

Thanks in advance.