我在SharePoint中有一個文檔庫。當新文件上傳到該庫時,我希望它自動複製到另一個文檔庫。我怎樣才能做到這一點?將文件複製到SharePoint中的文檔庫中
10
A
回答
14
使用項目事件接收器並覆蓋ItemAdded事件。 SPItemEventProperties將通過ListItem屬性爲您提供對列表項的引用。
有兩種方法可以做到這一點(感謝您發現CopyTo)。
方法1:使用CopyTo
這種方法複製任何列表項及其相關的文件和屬性的任何一個地址相同的網站集(可能是其他Web應用程序,以及,但我沒有測試過)。如果您查看項目的屬性或使用其下拉菜單,SharePoint自動維護到源項目的鏈接。此鏈接可通過UnlinkFromCopySource刪除。
CopyTo唯一的技巧是目標位置需要完整的URL。
public class EventReceiverTest : SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
properties.ListItem.CopyTo(
properties.WebUrl + "/Destination/" + properties.ListItem.File.Name);
}
}
方法2:流拷貝,手動設置屬性
,如果你需要在哪個項目屬性複製或如果文件內容需要修改更多的控制這種方法就只能是必要的。
public class EventReceiverTest : SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
SPFile sourceFile = properties.ListItem.File;
SPFile destFile;
// Copy file from source library to destination
using (Stream stream = sourceFile.OpenBinaryStream())
{
SPDocumentLibrary destLib =
(SPDocumentLibrary) properties.ListItem.Web.Lists["Destination"];
destFile = destLib.RootFolder.Files.Add(sourceFile.Name, stream);
stream.Close();
}
// Update item properties
SPListItem destItem = destFile.Item;
SPListItem sourceItem = sourceFile.Item;
destItem["Title"] = sourceItem["Title"];
//...
//... destItem["FieldX"] = sourceItem["FieldX"];
//...
destItem.UpdateOverwriteVersion();
}
}
部署
您有部署的各種選項,以及。您可以將事件接收器與連接到內容類型或列表的功能相關聯,並以編程方式添加它們。有關更多詳細信息,請參閱this article at SharePointDevWiki。
+0
請務必不要忘記複製元數據! – Colin 2009-06-29 17:04:06
相關問題
- 1. Sharepoint 2010:將文件複製或添加到文檔庫
- 2. 在Sharepoint中複製文檔
- 3. 將文件夾中的所有文件複製到文檔中
- 4. 將SharePoint文檔庫集成到ASP.NET中
- 5. SharePoint將網站的文檔庫中的圖像複製到另一個網站中的文檔庫
- 6. 如何將文件上傳到SharePoint中的文檔庫?
- 7. 更新Sharepoint文檔庫中的文件
- 8. Sharepoint文檔庫中的Eml文件
- 9. 修改SharePoint文檔庫中的文件
- 10. 是否可以將列表項目複製到SharePoint 2007中的文檔庫中?
- 11. VBA:將文本文件中的行復制到Word文檔中
- 12. 需要將複製sharepoint文件庫文件上的幫助存檔/其他文件庫sharepoint 2007
- 13. 使用WebClient將文檔上載到SharePoint文檔庫中的特定文件夾
- 14. Sharepoint從文檔庫中刪除文件
- 15. 文檔庫Sharepoint中的Versions.aspx
- 16. Sharepoint文檔庫文件
- 17. Sharepoint 2013將文件上傳到文檔庫文件大小
- 18. 在不同子網站(SharePoint)中的文檔庫之間複製Word文檔
- 19. 將文件從Unix複製到Sharepoint 2010
- 20. 將文件附加到Sharepoint 2010中的文檔中
- 21. 搜索A文檔庫或文檔在Sharepoint文檔庫中
- 22. 如何將Sharepoint文檔庫中的文檔返回給用戶?
- 23. 如何將文檔庫文檔複製到sharepoint 2007中的本地驅動器編程
- 24. Sharepoint文檔庫
- 25. 數據庫文件未複製到文檔目錄中
- 26. 如何在SharePoint文檔庫中製作Microsoft Word文檔「只讀」?
- 27. 複製文檔庫
- 28. 在SharePoint 2010中複製或移動文檔庫
- 29. 編程方式複製/移動文件夾/文件中的一個文檔庫到另一個文檔庫
- 30. Mongomapper:將文檔複製到新文檔中
在C#和SharePoint標記的上下文中,這個問題非常有意義 - 投票重新打開。 – 2015-09-01 22:09:31