2

I have implemented an event receiver on a SharePoint 2007 (MOSS) document library, to perform certain validations via the itemUpdating method and to copy the document to a document library in another site collection (and perhaps even an other farm, if possible?) via the itemUpdated method. The code looks like this:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(targetWebsiteUrl))
    {
        using (SPWeb web = site.OpenWeb())
        {
            var targetLibrary = web.Lists[targetDocLibName];
            using (Stream fileStream = file.OpenBinaryStream())
            {
                SPFile copiedFile = targetLibrary.RootFolder.Files.Add(fileName, fileStream);
            }
        }
    }
});

I am not sure if the above code works across farms, but it certainly copies the file to the destination website and document library. But I am unable to copy over any meta-data associated with the original document. Is there any way to copy over the meta-data, or is that a separate step and update?

1 Answer 1

3

Some pieces of answers:

  1. You canno't use that code to copy to another farm: this uses server-side API, and server-side API must be used on a server of the farm you're targetting. An option to push the file to farm B would be to use from server-side code on farm A client-side API to access farm B.
  2. Actually, that code, with privilèges elevation, can only work if you target another site collection in the same Web app as the source: the elevation reverts the identity back to the pool's identity, and if you target another Web app, it may not have needed permissions on the site collection B.
  3. To copy metadata while you add the file to the target, an option is to use that overload of .Add method: https://msdn.microsoft.com/fr-fr/library/office/ms480295(v=office.12).aspx. The hashtable contains a dictionary of property you want to set.
2
  • since I am working with SharePoint 2007 (MOSS), there is no client object model, if that is what you mean by "client side API", right?
    – Web User
    Commented Dec 26, 2015 at 16:37
  • Right, sorry for this. But you still have access to Web services, that should allow you to do the same job (see msdn.microsoft.com/en-us/library/aa979690(v=office.12).aspx)
    – Evariste
    Commented Dec 26, 2015 at 18:29

Your Answer

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

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