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 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();
share|improve this question
add comment

5 Answers

up vote 2 down vote accepted

Use SPFile.CopyTo

OR

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

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.

share|improve this answer
add comment

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;
}
share|improve this answer
    
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 Apr 12 '11 at 11:54
add comment

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

share|improve this answer
    
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. –  Mick Walker Apr 12 '11 at 13:10
    
I dont see how that should not work with content organizer? It doesnt care how metadata is set :) –  Anders Rask Apr 12 '11 at 15:25
add comment

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.

share|improve this answer
add comment

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.