Programming, Policitcs, and uhhh Pineapples.
# Sunday, January 07, 2007

A Note On Copying Files In WSS3

Sunday, January 07, 2007 7:52:33 PM UTC

I dunno if this was supported in WSS2 or not, but in WSS3, when a file is copied to a new destination, a link is stored which indicates where the new document is copied from.

In the database, you can see this by running the query:

SELECT
    tp_dirname,
    tp_leafname,
    tp_copysource,
    tp_hascopydestinations,
    tp_guid,
    *
FROM ALLUSERDATA

The tp_CopySource column holds the URL of the source document from which the given file is copied from.  If you simply change the URL here, you can point it to any file you want.  If the file is a copy, then the following information bar will be displayed on top of the file properties in the properties page:

copy-indicator.jpg

However, it's not so obvious how to do this programmtically.  First, I tried using the CopyTo() method of SPFile.  Aside from not having the desired effect, this method does not seem to allow copying files across site boundaries (for example, from a root site to a document workspace -- for that, you have to use the Add() method on the Files property of the target SPWeb).

On my second attempt, I tried to set the "Copy Source" property of the file.

foreach (object key in file.Properties.Keys) {
    Console.Out.WriteLine("{0} : {1}", key, file.Properties[key]);
}

Iterating through the properties of an SPFile instance, I found that one of the properties, was "Copy Source" (internal name of "_CopySource") and in fact, held the URL of the source document.  I tried to set this value and update the file, but this was unsuccessful yet again.

On my third attempt, I came across the CopyTo() method on the SPListItem class and this did it for me :-) Conveniently, it also allows you to copy an item across site boundaries.

I felt so stupid afterwards because it should have been obvious that I had to use the CopyTo() method on the SPItem because the SPItem is also where the UnlinkFromCopySource() method is located.

Friday, February 16, 2007 7:25:34 PM UTC
You are doing file operations (SPDocumentLibrary) which is why SPListItem.File != null.
SPListItem.File.MoveTo() and SPListItem.File.CopyTo() should have worked...

I am playing with Lists without files (such as announcements)
SPListItem.File == null

When I try SPListItem CopyTo() I recieve:
Microsoft.SharePoint.SPException: Source item cannot be found. Verify that the item exist and that you have permission to read it.
at Microsoft.SharePoint.SPCopy.CopyIntoNewItem(SPListItem src, SPFolder targetFolder, String targetUrl)
at Microsoft.SharePoint.SPCopy.CopyIntoItem(SPListItem src, String targetUrl)
at Microsoft.SharePoint.SPListItem.CopyTo(String destinationUrl)
at CopyMoveToParentFolder.Program.Main(String[] args)

Any ideas?
Ninja Coder
Friday, February 16, 2007 8:23:11 PM UTC
Ninja,

I looked into a bit to see what I could find. I spent a good 20-30 minutes on it and came away with nada - couldn't even add a new blank announcement to the list.

The CopyTo wouldn't make much sense since I couldn't think of any logical URL to provide for the announcement item (since DispForm.aspx?ID=# doesn't seem at all logical).

If I come across anything, I'll keep this post in mind and post an update :)
Chuck
Friday, May 11, 2007 6:37:43 PM UTC
Is this issue resolved, Iam getting the same error and Iam looking for a solution
Usha
Monday, May 21, 2007 1:42:31 PM UTC
hello everybody.. I encountered the same problem as Ninja Coder... and really i don't see were is the problem.. i used several kind of path, and always get the same error.

If anyone has solved this problem, please answer ! thanks a lot.
RV
Wednesday, January 16, 2008 2:08:20 PM UTC
I get it also, but I've written a custom list and this:

http://www.communardo.de/techblog/2008/01/08/sharepoint-listenelement-splistitem-in-eine-andere-liste-kopieren/

Should solve the problem. It's in german, look for the code (in italics).

Of course, it doesn't solve my problem since i'm trying to copy items across discussion boards and nothing seems to work right for that, but it does should solve most everyone elses.
Friday, November 21, 2008 6:31:47 AM UTC
Hi,
I too get the "Source item cannot be found. Verify that the item exists and that you have permission to read it." when trying to copy calendar items from one calendar to another.

Anyone came up with a resolution?

Thank you
regards

Chris Sammut
Chris
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview
RSS 2.0 Atom 1.0 CDF