6

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

2
  • 1
    I'm assuming you're on 2007 correct? The Content Organizer feature of 2010 handles this functionality I believe. Aug 17, 2011 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, 2011 at 9:04

3 Answers 3

7

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);
    }
2
  • 1
    Are you moving the Home.aspx file here?!?
    – Perplexed
    Apr 25, 2012 at 16:37
  • can we move the document to another web application's site collection with this code? Dec 5, 2016 at 9:41
1

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

0

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

1
  • 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, 2012 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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