Question

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?

Was it helpful?

Solution

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top