SharePoint list attachments, how to overwrite them
Quick question about SharePoint.... I need to update an attachment on a list using the SharePoint sdk,but when ever I delete the old one, and add the new one, the new document is never added. Below is my code...
/* Delete the attachment first and create a new attachment.*/
string fileName = newAttachmentName.Substring(0, newAttachmentName.IndexOf("."));
//Delete Attachment
开发者_StackOverflow社区 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();
Maybe this will be of any help:
Retrieving Attachments from SharePoint List Items
Below does NOT work:
SPAttachmentCollection attachments = listitem.Attachments;
foreach (SPFile file in attachments)
{
// Do something
}
Below DOES work:
SPFolder folder = web.Folders["Lists"].SubFolders[list.Title].SubFolders["Attachments"].SubFolders[listitem.ID.ToString()];
foreach (SPFile file in folder.Files)
{
// Something useful here
}
SPAttachmentCollection attachments1 = item.Attachments;
attachments1.Add(newAttachmentName, contents);
item.Update();``
where you overwriting your new file create new instance of Attachments( attachment1)
精彩评论