2

I am in the process of writing a custom event receiver. The basic flow is as follows:

  1. Document is added to Library

  2. Based on metadata of document, we check to see if a folder within another document library exists.

  3. If the folder does not exist, it is created.

  4. The newly added document is copied to the folder residing in another document library.

I have got myself to the point, where I can copy newly added files, from one document library to another when they are added. However I cannot figure out how to copy to a specific directory (by name) within a document library. Any help would be greatly received.

Here is my code so far:

SPFile sourceFile = properties.ListItem.File;
SPFile destFile; // Copy file from source library to destination         
using (Stream stream = sourceFile.OpenBinaryStream())
{
    var destLib = (SPDocumentLibrary) properties.ListItem.Web.Lists[listName];
    destFile = destLib.RootFolder.Files.Add(sourceFile.Name, stream);
    stream.Close();
} 
// Update item properties         
SPListItem destItem = destFile.Item;
SPListItem sourceItem = sourceFile.Item;
// Copy meta data
destItem["Title"] = sourceItem["Title"];
//...        
//... destItem["FieldX"] = sourceItem["FieldX"];        
//...         
destItem.UpdateOverwriteVersion();

5 Answers 5

2

Use SPFile.CopyTo

OR

//Ensure folder here
var destFolder = destLib.RootFolder.SubFolders["name"];
destFile = destFolder.Files.Add(sourceFile.Name, stream);
2
  • SPDocumentLibrary destLib = (SPDocumentLibrary)properties.ListItem.Web.Lists[listName]; var destFolder = destLib.RootFolder.Subfolders[folderName]; Microsoft.SharePoint.SPFolder' does not contain a definition for 'Subfolders' Commented Apr 12, 2011 at 11:51
  • 1
    'SubFolders' - small typo
    – gandjustas
    Commented Apr 12, 2011 at 12:21
2

Here's a utility method you could use. Do note that it does not consider any column data, so anything you need will have to be duplicated by hand. You could e.g. change it to return the newly created SPFile, and move whatever is required.

public static bool CopyFile(SPFile file, SPFolder targetFolder)
{
    Stream readStream = null;
    try
    {
        readStream = file.OpenBinaryStream();
        SPFile newFile = targetFolder.Files.Add(file.Name, readStream);
        foreach (object property in file.Properties.Keys)
        {
            if (newFile.Properties.ContainsKey(property) == false)
            {
                newFile.Properties.Add(property, file.Properties[property]);
            }
        }
        newFile.Update();
    }
    catch
    {
        return false;
    }
    finally
    {
        if (readStream != null)
        {
            readStream.Dispose();
        }
    }
    return true;
}
1
  • I may have been a bit quick in interpreting your question, but I'll just leave this here in either case. Might be of use to someone.
    – einaros
    Commented Apr 12, 2011 at 11:54
2

Hmm this functionality resemble the functionality provided by Content Organizer quite a bit! Is there a good reason to re-invent this functionality?

Content Organizer (amongst other things) lets you move documents from a drop-folder into other document library based on meta data -which in essence is what you describe as your requirement.

Check out this TechNet article for more info: http://technet.microsoft.com/en-us/library/ff608093.aspx

2
  • It does indeed, the reason I have opted for this method is we are using custom content types, and meta data is generated via OCR from the scanner. Commented Apr 12, 2011 at 13:10
  • I dont see how that should not work with content organizer? It doesnt care how metadata is set :) Commented Apr 12, 2011 at 15:25
1

Use the SubFolders property of the destlib.RootFolder (type SPFolder) property to get the sub-folders of the document library. Then use the Add method on the folder you're interested in adding the item into.

1

Unfortunately the Content Organiser does not work with documents that have been sourced via a WebDAV (e.g. drag and drop). There are scenario's were this code has to be developed.

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.