Wednesday, October 8, 2008

Accessing Attachments for an SPListItem

Today I was working on a project that needed some custom screens in SharePoint and manipulated a SharePoint list. I finished most of the screens to manipulate the list item itself and was about to move onto adding attachments to the list item. I began by try to iterate through each file in the SPAttachmentCollection that you can acess via the list item object. When I ran the page I kept getting an error that said it could not cast from string to SPFile object. I started commenting out lines to figure out where it was trying to cast a string into an SPFile because I did not do that explicitly in any of my code.



I finally got to the line that said For Each attachment As SPFile in AttachmentCollection. This was the line that was throwing the error. So I did some searching online and found that the SPAttachmentCollection only returns a string and not the actual attachments. The only way to get to the attachments is to navigate into the folder that holds the attachments.



The folder is located at Lists/{ListName}/Attachments/{ListItemID}. You can get to this programmatically in the following manner:



Dim folder As SPFolder = site.Folders("Lists").SubFolders(list.Title).SubFolders("Attachments").SubFolders(listItem.ID.ToString)



Once you have that folder you can iterate through each SPFile (that represents an attachment) and get back information such as the Url or the Name of the file. I used this syntax to render out the collection of attachments so the user could see them and open them. After figuring out this snag I was able to easily add attachments to a list item and then view the attachments for that list item.



As a side note, some people might be asking why I did not use the built-in New and Edit screens. I needed to be able to modify columns that were visible to certain groups of people. People that needed to review items had additional columns that they could fill out and those people that originally fill out the form should not see those columns. I tried accomplishing this in SharePoint Designer by customizing the New Form but I found that the attachment link no longer worked; it was throwing a Javascript error after I customized it, so I decided to create a custom screen that would allow me to run some security validations and redirect users to the proper form based on their group memberships.

Labels: , ,

3 Comments:

At March 2, 2009 at 3:27 PM , Anonymous Anonymous said...

I found a typo. "Attachments" is the correct syntax.

 
At March 3, 2009 at 8:09 AM , Blogger Michael Markel said...

Leo,

Thanks for the note about the typo. I have fixed it so that the syntax in the blog entry matches what the syntax should be.

Michael

 
At December 14, 2009 at 4:53 AM , Anonymous JonW said...

Even better way:
Use the .Rootfolder - property

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

This gives you the list rootfolder directly, and as a bonus it uses the nationalcharachters proof-name for the list, for ex: Förening => Frening. Given the Title-property the list's folder in your solution
caänt find the corresponding folder.

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home