Binding a Attachment to a Visual Webpart in sharepoint 2013

 private void GetMoreCEO()
        {
            using (SPSite osite = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb oweb = osite.OpenWeb())
                {
                    SPList olist = oweb.Lists.TryGetList("CEOMessage");
                    SPListItemCollection collListItems = olist.Items;

                    DataTable dt = new DataTable();
                    dt.Columns.Add("Attachments", typeof(string));
                    dt.Columns.Add("Title", typeof(string));
                    dt.Columns.Add("Message", typeof(string));              

                    foreach (SPListItem item in collListItems)
                    {
                        DataRow dr = dt.NewRow();
                        string attachmentAbsoluteURL = string.Empty;
                        if (item.Attachments.Count > 0)
                        {
                            foreach (String attachmentname in item.Attachments)
                            {
                                attachmentAbsoluteURL = item.Attachments.UrlPrefix + attachmentname;
                                dr["Attachments"] = attachmentAbsoluteURL.ToString();
                            }
                        }
                        else
                        {
                            dr["Attachments"] = "/_layouts/15/VrtxIntranetStyles/Images/NoImage.jpg";
                        }
                        dr["Title"] = item["Title"] != null ? item["Title"].ToString() : string.Empty;
                        dr["Message"] = item["Message"] != null ? item["Message"].ToString() : string.Empty;                    
                        dt.Rows.Add(dr);
                    }
                    gvMoreCEO.DataSource = dt;
                    gvMoreCEO.DataBind();
                }
            }
        }

Comments

Popular posts from this blog

My Interview Experience

React Checkbox Control SPFx