[英]How to add an item to a Document library from a list column in SharePoint?
我有一个列表,在其中一列中,我希望允许用户附加文档,然后将其发送到文档库。
这可能吗?
我已经能够从列表列中“查找”文档库中的项目,但是我想做相反的事情-从特定列中附加文档并将其转到文档库中。
您可以为此创建事件接收器。
样例代码:
/// <summary>
/// An attachment was added to the item.
/// </summary>
public override void ItemAttachmentAdded(SPItemEventProperties properties)
{
base.ItemAttachmentAdded(properties);
SPList list = properties.List;
SPListItem item = properties.ListItem;
SPAttachmentCollection attach = item.Attachments;
// document library to store attachment as document.
SPList listDoc = list.ParentWeb.Lists["MyDoc"];
for (int i = 0; i < item.Attachments.Count; i++)
{
if (i == item.Attachments.Count - 1)
{
string fileName = attach[i];// this is your last attachment name
SPFile file = list.ParentWeb.GetFile(attach.UrlPrefix + fileName);
string destFileUrl = listDoc.RootFolder.ServerRelativeUrl + "/" + fileName;
SPFile destFile = list.ParentWeb.Files.Add(destFileUrl, file.OpenBinary(), true/*overwrite*/);
//get the splistitem of the uploaded file
SPListItem finalItem = file.Item;
//update a field in the uploaded file if you want
//finalItem[""] = ;
//update the list item
finalItem.Update();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.