Wednesday, August 3, 2011

Getting Attachment from List Items

with the help of following Function you can get Attached files of that particular Item

public List<SPFile> GetListItemAttachments(SPListItem listItem)
        {
            try
            {
                List<SPFile> lstSPFile = null;
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    using (SPSite oSite = new SPSite(
                       listItem.ParentList.ParentWeb.Site.ID))
                    {
                        using (SPWeb oWeb = oSite.OpenWeb())
                        {
                            SPFolder folder = listItem.ParentList.RootFolder.
                               SubFolders["Attachments"].SubFolders[listItem.ID.ToString()];
                            if (null != folder)
                            {
                                lstSPFile = new List<SPFile>();
                                foreach (SPFile file in folder.Files)
                                {
                                    lstSPFile.Add(file);
                                }
                            }
                        }
                    }
                });
                return lstSPFile;
            }
            catch
            {
                return null;
            }
        }

No comments:

Post a Comment