How to show Attachments with DataFormWebPart

If you are using DataFormWebPart and you need to add the list of attachments to the output you have to modify the XSLT to use the AttachmentsField control.

If you simply use  

 <xsl:value-of select="@Attachments"/>

the result displayed will be a boolean (Yes/No) value. If you want to display the list of attachments with corresponding links you must use AttachmentsField control as described below. This control contains the logic needed to get the attachments and render the list with links.

First of all, locate the template with name dvt_1 and add the html code to insert a new column header like in the following example:

     <xsl:template name="dvt_1">
        <xsl:variable name="dvt_StyleName">Table</xsl:variable>
        <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/>
        <table border="0" width="100%" cellpadding="2" cellspacing="0">
            <tr valign="top">
                <xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
                    <th class="ms-vh" width="1%" nowrap="nowrap"></th>
                </xsl:if>
                <th class="ms-vh" nowrap="nowrap">Title</th>
                <th class="ms-vh" nowrap="nowrap">Author</th>
                <th class="ms-vh" nowrap="nowrap">Modified</th>
                 < thclass="ms-vh"nowrap="nowrap">Attachments</th > 
            </tr>
            <xsl:call-template name="dvt_1.body">
                <xsl:with-param name="Rows" select="$Rows"/>
            </xsl:call-template>
        </table>
    </xsl:template>

Now, locate the dvt_1.rowview template and add a new column with the AttachmentsField control:

 <xsl:template name="dvt_1.rowview">
  <tr>
      <xsl:if test="position() mod 2 = 1">
          <xsl:attribute name="class">ms-alternating</xsl:attribute>
      </xsl:if>
      <xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
          <td class="ms-vb" width="1%" nowrap="nowrap">
              <span ddwrt:amkeyfield="ID" 
                        ddwrt:amkeyvalue="ddwrt:EscapeDelims(string(@ID))" 
                        ddwrt:ammode="view">
              </span>
          </td>
      </xsl:if>
      <td class="ms-vb">
          <xsl:value-of select="@Title"/>
      </td>
      <td class="ms-vb">
          <xsl:value-of select="@Editor" disable-output-escaping="yes"/>
      </td>
      <td class="ms-vb">
        <xsl:value-of select="ddwrt:FormatDate(string(@Modified), 1040, 5)"/>
      </td>
      <td class="ms-vb">
          <xsl:element name="SharePoint:AttachmentsField">
          <xsl:attribute name="runat">server</xsl:attribute>
          <xsl:attribute name="ListId">
                  {D2C70ED5-8551-446D-B0CF-AC4B30387CF0}           
</xsl:attribute>
          <xsl:attribute name="FieldName">Attachments</xsl:attribute>
          <xsl:attribute name="ControlMode">Display</xsl:attribute>
          <xsl:attribute name="Visible">true</xsl:attribute>
          <xsl:attribute name="ItemId">
              <xsl:value-of select="@ID"/>
          </xsl:attribute>
          </xsl:element>
      </td>
  </tr>
</xsl:template>

Important: you must change the value of ListId attribute with your list id!

HTH

Dario

Technorati Tag: Sharepoint Designer,MOSS