I need to create an ItemUpdated event reciever on a documnt library that will detect when particular metadata value is updated on a document and move the document to a library in another site collection.

How can I programmatically move a document to a library in a different site collection? All of the other questions I can find on this site deal with moving the document to another library in the same site collection, and i know that SPFile.MoveTo() can be used for that but i need to move it to another site collection. It must be possible because it can be done through the SendTo link on the front-end.

Any ideas? TIA

link|improve this question

79% accept rate
1  
I'm assuming you're on 2007 correct? The Content Organizer feature of 2010 handles this functionality I believe. – PirateEric Aug 17 '11 at 16:56
Hi Eric, i'm using 2010. Could i programmatically to the file to the drop off library and then have it routed? – toby Aug 23 '11 at 9:04
feedback

2 Answers

up vote 2 down vote accepted

There's probably a few ways to do this. But the option I use is to actually read in the bytes of the source file and then write as a new file to the destination. Once you're happy the file is successfully copied, delete the source.

This can get a bit complex though if you want to maintain meta data and version history, but it's still doable.

Here's a simple example of just copying the file, without meta data or version history:

    Uri srcUrl = new Uri(@"http://sample.com/sitecol1/Pages/Home.aspx");
    Uri dstFolderUrl = new Uri(@"http://sample.com/sitecol2/Documents/");
    Uri dstFileUrl = new Uri(dstFolderUrl, Path.GetFileName(srcUrl.LocalPath));

    using (SPSite siteSrc = new SPSite(srcUrl.AbsoluteUri))
    using (SPSite siteDst = new SPSite(dstFolderUrl.AbsoluteUri))
    using (SPWeb webSrc = siteSrc.OpenWeb())
    using (SPWeb webDst = siteDst.OpenWeb())
    {
        SPFile srcFile = webSrc.GetFile(srcUrl.AbsolutePath);
        SPFolder dstFolder = webDst.GetFolder(dstFolderUrl.AbsolutePath);
        SPFile newFile = dstFolder.Files.Add(dstFileUrl.AbsolutePath, srcFile.OpenBinary());
        //Optional : Check-in
        newFile.CheckIn("", SPCheckinType.MajorCheckIn);
    }
link|improve this answer
Are you moving the Home.aspx file here?!? – Perplexed Apr 25 at 16:37
feedback

Since you're on 2010, you can take advantage of the content organizer to automatically route documents based on metadata or content type.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.