Showing posts with label Delete Attachment. Show all posts
Showing posts with label Delete Attachment. Show all posts

Saturday, October 3, 2009

How to Delete attachment from an Item in Sharepoint List

public static void DeleteAttachements(int id)

{
using (SPSite site = new SPSite("http://portal"))
{
using (SPWeb web = site.OpenWeb())
{
SPListItem listItem = web.Lists["mylist"].GetItemById(id);
List<string> fileNames = new List<string>();
foreach (string fileName in listItem.Attachments)
{
fileNames.Add(fileName);
}
foreach(string fileName in fileNames)
{
listItem.Attachments.Delete(fileName);
}
listItem.update();
}
}
}