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

Friday, 13 June 2014

Retrieve Attachments from SharePoint List Programmatically

private void RetrieveFile(SPListItem nitem)
        {
            DataTable dthref = new DataTable();
            dthref.Columns.Add("ViewFile", typeof(string));
            DataRow dataRow;
            dataRow = dthref.NewRow();
            foreach (String attachmentname in nitem.Attachments)
            {
                String attachmentAbsoluteURL = nitem.Attachments.UrlPrefix + attachmentname;
                //SPFile attachmentFile = SPContext.Current.Web.GetFile(attachmentAbsoluteURL);
                HtmlAnchor htmlanchor = new HtmlAnchor();
                htmlanchor.HRef = attachmentAbsoluteURL;
                dthref.Rows.Add(attachmentAbsoluteURL);
                // ViewState["attachmentFile"] = attachmentFile;
                ViewState["attachmentAbsoluteURL"] = attachmentAbsoluteURL;

            }

            dthref.AcceptChanges();
            gvFileView.Visible = true;
            gvFileView.DataSource = dthref;
            gvFileView.DataBind();
        }
Please provide your feedback in the comments section above. Please don't forget to follow.