0

I have written code where it will show the Sharepoint document library in sp grid view.

In the grid view it will show the doc icon Image correctly. Now i want to Download the uploade document from this when clicked on Doc icon.

Here is the code and Out put of the Image

    SPGridView gv = new SPGridView();
                gv.ID = "gv";
                gv.EmptyDataText = "No items found!";
                gv.AllowSorting = true;
                gv.AllowFiltering = true;
                gv.AutoGenerateColumns = false;
                gv.EnableSortingAndPagingCallbacks = true;
                CreateBoundField("Type", "Type", false, gv);
                CreateBoundField("Title", "Title", false, gv);
                CreateBoundField("Date", "Date", false, gv);
                CreateBoundField("ProcessTask", "ProcessTask", false, gv);
                DataTable dt = new DataTable();
                SPWeb web = SPContext.Current.Web;
                SPListItemCollection items = web.Lists["Shared Documents"].Items;            
                dt.Columns.Add("Type");
                dt.Columns.Add("Title");
                dt.Columns.Add("Date");
                dt.Columns.Add("ProcessTask");
                foreach (SPListItem item in items)
                {
                    DataRow dr = dt.NewRow();
                    string value = item["ProcessTask"].ToString();
                    value = value.Substring(value.LastIndexOf(";#") + 2);
                    string docicon = SPUtility.ConcatUrls("/_layouts/images",
                    SPUtility.MapToIcon(item.Web, SPUtility.ConcatUrls(item.Web.Url, item.Url), "", IconSize.Size16));
                    dr["Type"] = string.Format("<img src='{0}' />", docicon);
                    dr["Title"] = item.Title;
                    dr["Date"] = item["Created"].ToString().Split(' ')[0];
                    dr["ProcessTask"] = value;
                    dt.Rows.Add(dr);
                }
                gv.DataSource = dt;
                gv.DataBind();
                this.Controls.Add(gv);
               SPListItemCollection _GridCollection = SPContext.Current.Web.Lists["Shared Documents"].Items;
                DataTable _MyDatabale = _GridCollection.GetDataTable();
                gvdetails.DataSource = _MyDatabale;
                gvdetails.DataBind();

}

private void CreateBoundField(string sDataField, string sHeaderText, bool bReadOnly, SPGridView theGridView)
    {
        BoundField field = new BoundField();
        field.DataField = sDataField;
        field.HeaderText = sHeaderText;
        field.HtmlEncode = false;
        field.HeaderStyle.ForeColor = ColorTranslator.FromHtml("#808080");
        field.HeaderStyle.Font.Names = new string[] { "verdana", "arial", "helvetica", "sans-serif" };
        field.ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
        if (sHeaderText.ToLower().Equals("type"))
        {
            field.ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
            field.ItemStyle.Width = Unit.Pixel(35);
            field.HeaderStyle.Width = Unit.Pixel(35);
        }
        field.ReadOnly = bReadOnly;
        if (!bReadOnly)
            field.SortExpression = sDataField;
        field.Visible = true;
        theGridView.Columns.Add(field);
    }  

enter image description here

  • 1
    I'm sorry but it's exceptionally hard to understand your objective. Are you asking how to add a hyper-link to the page you are rendering so that users can download the file or documnt associated with the gridview's row? – Robert Kaucher Jan 19 '14 at 2:47
  • That is how I am reading it Robert – Eric Alexander Jan 19 '14 at 3:51
3

You need to get hold of the SPFile object, which is a property of a ListItem item, and get the ServerRelativeUrl property. Add this to an <a> tag and you're done.

//the rest of your code omitted for brevity
foreach (SPListItem item in items)
{
    SPFile file = item.File;
    string relativeUrl = file.ServerRelativeUrl; //returns a string in the form /Site_Name/Subsite_Name/Folder_Name/File_Name

    //surround your img tag with an anchor tag
    dr["Type"] = string.Format("<a href='{0}'><img src='{1}' /></a>", relativeUrl, docicon);
}
  • Wow Great i think i am close now .. Can we have a check box enable before the File As we get in OOB sharepoint document library ?? – santosh kondapalli Jan 19 '14 at 17:18
  • can we get through jquery – santosh kondapalli Feb 18 '16 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.