Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Sunday, 1 June 2014

Display SharePoint List Attachments of Each Item In GridView

in aspx page:

<asp:GridView ID="GridView1" runat="server" ShowHeader="false" Visible="false" AutoGenerateColumns="false">
                <Columns>
                    <asp:HyperLinkField DataTextField="Title" DataNavigateUrlFields="URL"></asp:HyperLinkField>
                </Columns>
            </asp:GridView>

in code behind:

try
            {
               SPWeb oWeb = SPContext.Current.Web;
                SPListItem nitem = oWeb.Lists["UrListName"].GetItemById(id);
                DataTable table = new DataTable();
                table.Columns.Add("Title", typeof(string));
                table.Columns.Add("URL", typeof(string));
                foreach (String attachmentname in nitem.Attachments)
                {
                    DataRow row = table.NewRow();
                    row["Title"] = attachmentname;
                    row["URL"] = nitem.Attachments.UrlPrefix + attachmentname;
                    table.Rows.Add(row);
                }
                table.AcceptChanges();
                GridView1.Visible = true;
                GridView1.DataSource = table.DefaultView;
                GridView1.DataBind();
            }
   catch (Exception ex)
            {
                //throw ex;
            }

Please provide your feedback in the comments section above. Please don't forget to follow.