Working on Attachment object SPFile in a share point list
Posted on 9/15/2008 4:47:44 AM
in #SharePoint
Hi,
/SPAN>/P>
While working with share point, many a times, when you are working with list item you have attachments with the list item. Many a times you want to programmatically access these attachment files (as SPFile objects). The SPListItem also exposes a collection of all the Attachments with the help of the property Attachments. But this list is nothing but a collection of string containing the filename of the Attachment./SPAN>/P>
/SPAN>/P>
To get to work with the actual SPFile object for each attachment, this will not help much. There are two way to get the SPFile object for the attachments./SPAN>/P>
/SPAN>/P>
SPFolder folder = web.Folders["Lists"].SubFolders[list.Title].SubFolders["Attachments"].SubFolders[listitem.ID.ToString()];/o:p>/SPAN>/P>
/o:p>/SPAN>/P>
Next you can easily iterate through all the Files in the folder like this./o:p>/SPAN>/P>
/o:p>/SPAN>/P>
foreach (SPFile file in folder.Files) /SPAN>{ /SPAN>////Work with SPFile object here. /SPAN>}/o:p>/SPAN>/P>
/SPAN>/o:p>/SPAN>/P>
Another way is to simple use the following code./o:p>/SPAN>/P>
/o:p>/SPAN>/P>
foreach (string fileName in item.Attachments) { SPFile file = item.ParentList.ParentWeb.GetFile( item.Attachments.UrlPrefix + fileName); /o:p>/SPAN>/P>
/SPAN>////Work with SPFile object here./o:p>/SPAN>/P>
}/o:p>/SPAN>/P>
/o:p>/SPAN>/P>
Vikram/o:p>/SPAN>/P>
|