Search This Blog

Friday, September 11, 2015

How to take document icons from SharePoint 2013

        protected void grdDocuments_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DataRowView dr = e.Row.DataItem as DataRowView;
                    string iconUrl = DocumentBL.GetIconUrl(dr["File_x0020_Type"].ToString());
                    string docurl = "/" + dr["FileRef"].ToString().Split('#')[1];

                    Image iconImg = e.Row.FindControl("iconImage") as Image;
                    HtmlAnchor aImage = (HtmlAnchor)e.Row.FindControl("aIconImage");

                    if (iconImg != null)
                    {
                        iconImg.ImageUrl = iconUrl;
                        aImage.HRef = docurl;
                    }

                    e.Row.Cells[3].Text = Convert.ToDateTime(dr["Modified"]).Date.ToString("yyyy-MM- dd");
                }
            }
            catch (Exception ex)
            {
             
                throw;
            }
        }

        public static string GetIconUrl(string icon)
        {
            SPWeb web= SPContext.Current.Web;
            string docIcon = SPUtility.MapToIcon(web, icon, string.Empty, IconSize.Size16);
            return string.Format("{0}/_layouts/images/{1}", web.Url, docIcon);
        }

No comments:

Post a Comment