Tuesday, February 21, 2012

Get all attachments from SharePoint List

using (SPSite oSPsite = new SPSite("http://sitename/"))
{
using (SPWeb oSPWeb = oSPsite.OpenWeb())
{
oSPWeb.AllowUnsafeUpdates = true;

// Fetch the List
SPList list = oSPWeb.Lists["TestCustomList"];

SPListItemCollection itemCol = list.Items;

foreach (SPListItem item in itemCol)
{
SPAttachmentCollection attachments = item.Attachments;

foreach (string fileName in attachments)
{

SPFile file = oSPWeb.GetFile(attachments.UrlPrefix + fileName);
Console.Write("file.Url " + file.Url + "\n");
Console.Write("Name:" + file.Name + "\n");
}

}

oSPWeb.AllowUnsafeUpdates = false;
}
}

Followers