开发者

Custom Event Receiver - Copy To Folder

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 doc开发者_StackOverflow社区ument 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();


Answer

//Ensure folder here 

var destFolder = destLib.RootFolder.SubFolders["name"]; 

destFile = destFolder.Files.Add(sourceFile.Name, stream);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜