A Note On Copying Files In WSS3

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:

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.

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.

You may also like...

6 Responses

  1. Ninja Coder says:

    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?

  2. Chuck says:

    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 šŸ™‚

  3. Usha says:

    Is this issue resolved, Iam getting the same error and Iam looking for a solution

  4. RV says:

    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.

  5. 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.

  6. Chris says:

    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