Skip to main content Home Help (new window) Eric Shupps
Go Search
Home
Blogs
Company
Products
Services
Current News
Idera Acquires Sonar from BinaryWave
BinaryWave Announces SharePoint Performance Optimization Service
Upcoming Events
Home > Blogs > Eric Shupps > Posts > Retrieving Attachments from SharePoint List Items
Retrieving Attachments from SharePoint List Items
This one had me beating my head against the wall for quite a while so I'm sharing it to save you the cost of a handful of Tylenol (or Paracetamol if you're on the other side of the pond).
 
Each item in a SharePoint list has an associated SPAttachmentCollection array.  It would seem logical that this array would function like most of the other object arrays in SharePoint and be comprised of SPAttachment or, more likely, SPFile objects so we could do the following:
 
SPAttachmentCollection attachments = listitem.Attachments;
 
foreach (SPFile file in attachments)
{
  // Do something
}
 
No soup for you, dear reader, as doing that throws an invalid cast exception.  Surprised?  Well, so was I and it took me quite a while to figure out that the SPAttachmentCollection is simply an array of strings representing the file name of each attachment.  Oh, joy, just what I needed.  I'm not sure what good that does anyone, as there are actually several useful methods associated with the SPAttachmentCollection, like Add(), Delete(), and Recycle(), but there you have it.
 
So how do you get to the actual files themselves?  Not easily  or logically, I assure you.  Here's the way it's done:
 
SPFolder folder = web.Folders["Lists"].SubFolders[list.Title].SubFolders["Attachments"].SubFolders[listitem.ID.ToString()];
 
Then you can do:
 
foreach (SPFile file in folder.Files)
{
  // Something useful here
}
 
Crazy, no? 
 
 

Comments

Thanks for sharing the tip

It has saved me lot of time trying to find the information.
at 11/4/2007 11:17 PM

This might be a bit easier...

foreach (string fileName in item.Attachments)
  {
    SPFile file = item.ParentList.ParentWeb.GetFile(
      item.Attachments.UrlPrefix + fileName);
  }
at 12/5/2007 12:19 AM

I can't thank you enough

Google really needs to index your site better.  I've been searching for SPAttachmentCollection and SPFile all night and have just found your post!

Thank you for helping me out!
at 5/1/2008 3:22 PM

Not bad

Well It looks stupid but it's the only way to get item attachments

M Guys are not that smart heh

....
at 5/11/2008 2:14 PM

VS: This might be a bit easier...

Jep, that´s the easiest way..
at 6/14/2008 12:07 PM

Thank you

I like it,absolutely
at 9/11/2008 3:23 AM

Thank you

This is a really good post, thanks
at 9/11/2008 2:02 PM

Many thanks

This post rules.  Thought I was going nuts.
at 9/24/2008 1:05 PM

Good Article

Good Article....
at 11/8/2008 7:08 AM

Re: Retrieving Attachments from SharePoint List Items

Hi Eric,

Good post. Its woking for me..

Thanks
at 4/17/2009 2:01 PM

Excellent !

thank you for sharing tips
at 5/7/2009 4:03 AM

step by step

hi there!! I am a system administrator with little knowledge of coding... can you tell me you to do this.?

thanks
at 5/29/2009 2:47 PM

A fix

Here is a code fix

SPFolder folder = web.Folders["Lists"].SubFolders[list.RootFolder.Name].SubFolders["Attachments"].SubFolders[item.ID.ToString()];
at 8/26/2009 9:30 AM

Grey out items

1. With regards to the WebPart, if I click on the link it opens the page in the webpart window, how do I get this to open in a new window?

2. Do you know if it is possible to ‘Grey’ out certain columns in a task list so that only certain users can update them?
 
3. Do you know if when you create a new item in a task list if SP can automatically create a document workspace for this task with all the information (document library, structure, etc) from the other workspace?
at 11/4/2009 8:31 AM

Awesome

Works good.  We're still using SharePoint 2003 --- so I need stuff like this. Thanks!
at 12/1/2009 4:02 PM

Great post

Thanks, it saved me a lot of time!
at 12/11/2009 2:42 AM

Better way, international characters

var folder = list.RootFolder.SubFolders["Attachments"].SubFolders[listitem.ID.ToString()];

Rootfolder gives the folder for the list directly.
Good when for ex: the list contains national characters, and the original proposed solution results in a bug *(the folder corresponding to the title can not be found)
at 12/14/2009 3:40 AM

how to retrive image as attachment

it works fine in documents as attachments

I am trying to get the images from list item

it gives error like "Length cannot be less than zero. Parameter name: length "

Please help!!!!!
at 12/31/2009 3:55 AM

just a SP user

Hi, I am just a user who gathered files as attachemnts to the list, and then disabled the setting to allow the user attach the files. so all the files seem to disappear. Did they? or is there a way to get to them?  its suggested that a code can retrive the attachments, but how do I get tot he code?
Any help is appriciated, i am going crazy
at 1/21/2010 4:00 PM

Drop this code where?

Exactly step by step where are you dropping any of these code examples? There are many opinions, blogs, links to do this with a dwp accessing a linked source list, but every post I've seen is not exact enough to pull it off in one shot.
at 2/10/2010 2:00 PM

Cool but...

I seem to get access denied using the method described in the post when run by a user without admin rights (even while running with elevated permissions!)

The code supplied in one of the comments helped me greatly, however.

foreach (string fileName in item.Attachments)
  {
    SPFile file = item.ParentList.ParentWeb.GetFile(
      item.Attachments.UrlPrefix + fileName);
  }
at 2/15/2010 12:32 PM

Hiding/Disabling delete function for attachments

I have a list with attachments.  Upon opening an item for edit, the attachments are listed at the bottom of the edit form.  Next to the attachment is a 'delete' function.  One of my users has requested that I hide or disable the 'delete' function to prevent certain permission groups with update perms to NOT have the option to delete the attachment.  Can someone help me?
at 3/31/2010 7:51 AM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Your Name


Your URL


Comment Date *

To prevent spam from automated bots please provide a valid date in the format "MM/DD/YYYY".
Attachments