如何从SharePoint中的列表列向文档库添加项目?

时间:2018-02-02 18:28:44

标签: sharepoint

我有一个列表,在其中一个列中,我想让用户附加一个文档,然后将其发送到文档库。

这可能吗?

我已经能够从列表列中“查找”文档库中的项目,但我想做相反的事情 - 从特定列附加文档并将其转到文档库。

1 个答案:

答案 0 :(得分:0)

您可以为此创建事件接收器。

示例代码:

/// <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();

                }

            }
        }