SharePoint列表附件,如何覆盖它们

时间:2010-08-12 18:57:30

标签: sharepoint

关于SharePoint的快速问题.... 我需要使用SharePoint sdk更新列表中的附件,但是当我删除旧附件并添加新附件时,永远不会添加新文档。以下是我的代码......

/* Delete the attachment first and create a new attachment.*/
            string fileName = newAttachmentName.Substring(0, newAttachmentName.IndexOf("."));
            //Delete Attachment
            SPAttachmentCollection attachments = item.Attachments;
            if (item.Attachments != null)
            {
                string oldfilename = attachments[0].ToString();

                attachments.DeleteNow(oldfilename);
                item.Update();
            }

            //AddAttachement(item, newAttachmentName, attachmentStream, true);
            attachments.Add(newAttachmentName, contents);
            ////attachments[0] = filename;
            item.Update();

2 个答案:

答案 0 :(得分:2)

也许这会有任何帮助:

Retrieving Attachments from SharePoint List Items

以下不起作用:

SPAttachmentCollection attachments = listitem.Attachments;

foreach (SPFile file in attachments)
{
  // Do something
}

以下工作:

SPFolder folder = web.Folders["Lists"].SubFolders[list.Title].SubFolders["Attachments"].SubFolders[listitem.ID.ToString()];
foreach (SPFile file in folder.Files) 
{   
    // Something useful here 
}

答案 1 :(得分:0)

SPAttachmentCollection attachments1 = item.Attachments;
attachments1.Add(newAttachmentName, contents);
item.Update();``

您覆盖新文件的位置会创建新的附件实例(attachment1)