Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
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

3 Answers 3

up vote 4 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);
    }
share|improve this answer
    
Are you moving the Home.aspx file here?!? –  Perplexed Apr 25 '12 at 16:37

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

share|improve this answer

Most of the upload code uses CopyTo() method, but progmatically uploaded (or we should say copied) documents will not be routed immediately. It will wait for content organizer timer job to run, which is weekly by default or may be daily. There is a better way by using OfficialFileSubmit() method which uploads files and trigger routing rules straightaway.

Uploading Documents Programatically to a Content Organizer Enabled Site

share|improve this answer
1  
Hi pratap, welcome to SharePoint StackExchange, while you blog may answer the question it doesn't state why it might. Could you please elaborate. –  Hugh Wood Nov 26 '12 at 9:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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