Tuesday, July 7, 2009

Programmatically Get SPList Attachments

Here is the code snippet to get the Attachments of a given SPList. You can modify the code to return a collection instead.

public void GetListAttachments(string site, string listName)
{
//StringBuilder sAttNames = new StringBuilder();
using (SPSite oSite = new SPSite(site))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
SPList oList = oWeb.Lists[listName];

SPListItemCollection oListItems = oList.Items;
foreach (SPListItem item in oListItems)
{
SPAttachmentCollection collAttachments = item.Attachments;

if (collAttachments.Count <= 0) return string.Empty;

foreach (var attachment in collAttachments)
{
//sAttNames.Append(attachment.ToString());
}
}
}
}
//return sAttNames.ToString();
}


No comments: