SharePoint - How to display attachment field in a SPGridView

Asked By Bookworm on 04-Jan-12 11:17 AM
Hi,

i need to display some fields of List1 in a SPGridVie, everything is fine with the others fields, but i donk know what to to
to display the attachment field with the documents that are attached.
pls is very urgent to me, if you have smth that can help!

thnx
Riley K replied to Bookworm on 04-Jan-12 11:24 AM


create a new type of BoundField class. 

public class AttachmentAwareSPBoundField : SPBoundField
{
  protected override void ChildControlDataBinding(Control childControl, object dataItem,
                          MemberDescriptor dataFieldPropertyDescriptor)
  {
    PlaceHolder placeHolder = (PlaceHolder) childControl;
    string propertyValueAsHtml = GetPropertyValueAsHtml(dataItem, dataFieldPropertyDescriptor.Name);
    if (!string.IsNullOrEmpty(propertyValueAsHtml))
    {
      bool hasAttachment;
      if (bool.TryParse(propertyValueAsHtml, out hasAttachment))
      {
        if (hasAttachment)
        {
          Image image = new Image();
          image.ImageUrl = "/_layouts/images/attach.gif";
          placeHolder.Controls.Add(image);
        }
      }
      else
      {
        Label label = new Label();
        label.Text = SPHttpUtility.NoEncode(propertyValueAsHtml);
        placeHolder.Controls.Add(label);
      }
    }
  }
 
  protected override Control GetChildControlInstance()
  {
    return new PlaceHolder();
  }
}

Now in the place of bound field you can place this 

<MyControls:AttachmentAwareSPBoundField DataField="Attachments" HeaderText="" SortExpression="Attachments" HeaderImageUrl="images/attach.gif"/>


http://sharepoint.stackexchange.com/questions/23919/attach-document-from-sharepoint-list-to-gridview 
http://stackoverflow.com/questions/8242819/attach-sharepoint-2010-list-attachment-to-gridview 

Regards
[)ia6l0 iii replied to Bookworm on 04-Jan-12 12:35 PM
I would rather choose to bind the URL/ServerRelativeUrl property of the SPFile in the grid.  It would give you the path of the file Like: sitename/subsite/folder/file
kalpana aparnathi replied to Bookworm on 04-Jan-12 02:37 PM
When using the SPGridView to bind to an SPDataSource, you define columns i.e.

<SharePoint:SPGridView runat="server" ID="oustandingTasksGrid" AutoGenerateColumns="false" AllowFiltering="true" AllowSorting="true" > <Columns> <SharePoint:SPBoundField DataField="ID" HeaderText="Task No." SortExpression="ID" /> <SharePoint:SPBoundField DataField="Attachments" HeaderText="Task No." SortExpression="Attachments" /> <SharePoint:SPMenuField NavigateUrlFields="ID" NavigateUrlFormat="../Lists/Tasks/DispForm.aspx?ID={0}" TextFields="Title" MenuTemplateId="outstandingTaskMenu" HeaderText="Short Description" TokenNameAndValueFields="ID=ID" SortExpression="Title" /> 


However, the Attachments field will be rendered with a "TRUE" or "FALSE" instead of the little attachment (paperclip) symbol - to fix this, the easiest way I found was to create a new type of BoundField class.

The Code



[SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]

public class AttachmentAwareSPBoundField : SPBoundField { protected override void ChildControlDataBinding(Control childControl, object dataItem, MemberDescriptor dataFieldPropertyDescriptor) { PlaceHolder placeHolder = (PlaceHolder) childControl; string propertyValueAsHtml = GetPropertyValueAsHtml(dataItem, dataFieldPropertyDescriptor.Name);

if (!string.IsNullOrEmpty(propertyValueAsHtml))
{
bool hasAttachment;
 if (bool.TryParse(propertyValueAsHtml, out hasAttachment))
{
 if (hasAttachment)
{
Image image = new Image(); image.ImageUrl = "/_layouts/images/attach.gif"; placeHolder.Controls.Add(image);
}
 }
 else
{
Label label = new Label();
 label.Text = SPHttpUtility.NoEncode(propertyValueAsHtml); placeHolder.Controls.Add(label);
 }
 }
}

protected override Control GetChildControlInstance()
{
 return new PlaceHolder();
 }
 }


Usage

You can now replace this:

<SharePoint:SPBoundField DataField="Attachments" HeaderText="Task No." SortExpression="Attachments" /> 


With this:

<MyControls:AttachmentAwareSPBoundField DataField="Attachments" HeaderText="" SortExpression="Attachments" HeaderImageUrl="images/attach.gif"/> 


And get a pretty little attachment icon, shame it doesn't work this way out of the box.