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?
It has saved me lot of time trying to find the information.
foreach (string fileName in item.Attachments)
{
SPFile file = item.ParentList.ParentWeb.GetFile(
item.Attachments.UrlPrefix + fileName);
}
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!
Well It looks stupid but it’s the only way to get item attachments
M Guys are not that smart heh
….
Jep, that´s the easiest way..
I like it,absolutely
This is a really good post, thanks
This post rules. Thought I was going nuts.
Good Article….
Hi Eric,
Good post. Its woking for me..
Thanks
thank you for sharing tips
thank you for sharing tips
hi there!! I am a system administrator with little knowledge of coding… can you tell me you to do this.?
thanks
Here is a code fix
SPFolder folder = web.Folders[“Lists”].SubFolders[list.RootFolder.Name].SubFolders[“Attachments”].SubFolders[item.ID.ToString()];
Works good. We’re still using SharePoint 2003 — so I need stuff like this. Thanks!
Thanks, it saved me a lot of time!
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)
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!!!!!
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!!!!!
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
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
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.
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.
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);
}
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?
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?
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);
}
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);
}
I couldn’t have figured this out without your help. Thanks.
Just had to comment that while the post is very useful the vscroll bar is missing from this page/site. navagating below the fold was quit a challence. Scroll wheel didn’t work scroll lock and up and down didn’t work. I settled on clicking and dragging to see the content below the fold.
You just saved me hours of pain.
THANKS!!!!
I don’t often post. This not only answers the question of how to get to the attachment files, but also gives some insight into how SP “organizes” listitem attachments.
Really helped.. In content migration scenario
Please forgive my ignorance; where do I put this code,
“SPFolder folder = web.Folders[“Lists”].SubFolders[list.Title].SubFolders[“Attachments”].SubFolders[listitem.ID.ToString()];”
Then where do I do,
“foreach (SPFile file in folder.Files)
{
// Something useful here
}”
Thank you so much 🙂