【问题标题】:How to add an item to a Document library from a list column in SharePoint?如何从 SharePoint 中的列表列将项目添加到文档库?
【发布时间】:2018-02-05 13:48:11
【问题描述】:

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

这可能吗?

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

【问题讨论】:

    标签: sharepoint


    【解决方案1】:

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

    示例代码:

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

    【讨论】:

      猜你喜欢
      • 2023-02-12
      • 2015-01-23
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 2011-01-09
      相关资源
      最近更新 更多