Get Attachments from SharePoint List

Get Attachments from SharePoint List:-

In this post you will learn to get all attachment from SharePoint list.we can have multiple attachments against a list item, it has always been a requirement to Get these attachments and gets it displayed within a custom web page Programmatically.

The code is here.:-

[AdSense-A]

public class AttachmentClass
        {
            public int ID { get; set; }
           
            public string Attchmnt { get; set; }
        }       
        public void BindRepeater()
        {
            try
            {
                List<AttachmentClass> AttachmentList = new List<AttachmentClass>();
                using (SPSite site = new SPSite("SiteURL"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        string attuploaded = string.Empty;
                        SPList spList = web.Lists.TryGetList("ListName");
                        SPListItemCollection items = spList.GetItems();
                        if (items != null)
                        {
                            foreach (SPListItem item in items)
                            {
                                AttachmentClass dataItems = new AttachmentClass();
                                // dataItems.ID = Convert.ToInt32(item["ID"].ToString());
                                //var itemid = item["ID"];
                                string[] _Str_Attachments = GetAttachmentCollection(item);
                                string tt = string.Empty;

                                if (_Str_Attachments != null)
                                {

                                    int len = _Str_Attachments.Length;

                                    SPFolder sourceItemAttachmentsFolder = item.Web.Folders["Lists"].SubFolders[item.ParentList.Title].SubFolders["Attachments"].SubFolders[item.ID.ToString()];

                                    foreach (SPFile file in sourceItemAttachmentsFolder.Files)
                                    {
                                        attuploaded = file.Name.ToString();
                                        var parent = file.ParentFolder.ToString();
                                        var urllist = file.Web.Url.ToString();
                                        tt += "<li><a href='" + urllist + "/" + parent + "/" + attuploaded + "'>" + attuploaded + "</a><span onclick='lbldelete1(this)' style='color:red; font-weight:bold;'></span></li>";

                                    }
                                        dataItems.ID = item["ID"].ToString();
                                   
                                    dataItems.Attchmnt = tt;
                                    AttachmentList.Add(dataItems);
                                }

                            }
                            if (AttachmentList.Count > 0)
                            {
                                rpt.DataSource = AttachmentList;
                                rpt.DataBind();

                            }
                            else
                            {
                                AttachmentList.Add(new AttachmentClass());
                                rpt.DataSource = AttachmentList;
                                rpt.DataBind();
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
 public string[] GetAttachmentCollection(SPListItem item)
        {
            try
            {
                string FileNames = String.Empty;
                if (item.Attachments.Count > 0)
                {
                    string[] _Str_File_Name = new string[item.Attachments.Count];
                    for (int i = 0; i < item.Attachments.Count; i++)
                    {
                        SPFile file = item.ParentList.ParentWeb.GetFile(item.Attachments.UrlPrefix + item.Attachments[i].ToString());
                        _Str_File_Name[i] = file.Name;
                    }
                    return _Str_File_Name;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return null;
        }

 


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!