Tuesday, February 16, 2010

Displaying Attachments to SharePoint Blog Posts

Where I work, we are interested in creating online "learning histories" of our activities.  We conceive of the learning history as a set of brief reflective essays, usually contemporaneous with the events they describe, and sometimes accompanied by (or accompanying) document artifacts. This sort of sounds like a blog with attachments, especially because blogs put chronological organization in the foreground.  

We would like to add entries in the learning history by email as well as via web interface.  So I set up a SharePoint blog, enabled attachments on the "Posts" list, email-enabled the list, and sent an email with attachments.  When viewed via AllItems.aspx, the paper-clip icon was present, but the attachments did not appear when the post was displayed on the Home page or the Post.aspx page.  They did, however, appear on the EditPost.aspx page.  So I knew they were in the system.  [See more information on this in my post "Emailing to SharePoint Blogs".]

I googled and found a post <http://www.pampigt.se/?p=69> that seemed to address the question directly.  The first part of the post deals with changing the site definition of blogs so that "Add Attachment" appears in the "New" and "Edit" page toolbars.  Since I don't have access to the 12 hive for this site, and I know I can get attachments in via email, I skipped to the next step, which reads:


If you want to display the attached files in your post please convert the Web Part in your post.aspx that shows the “post” to an XSLT DVWP.
Fix the Author problem by removing the = character before =@Author and thenfollow the guide below:

Now you have to open the post.aspx of your SharePoint 2007 blog and add the code linked from :

http://blogs.msdn.com/dmp/archive/2009/01/21/how-to-show-attachments-with-dataformwebpart.aspx

This is a little sketchy, so here is the detail as I experienced it:

The conversion referred to in the first sentence is accomplished in SharePoint Designer.  I started this work by opening the Post.aspx page in SPD, then before doing anything else I saved it under another name (Post-test.aspx).  I did all my testing on this page.  This way, I could test in the live site without interfering with production.

To convert the List View Web part to a Data View (XSLT) Web Part, open the page, select the "Posts" web part (which is displaying "There are no posts in this category."), right-click, and choose "Convert to XSLT".

SPD will complain that it can't display the web part. This is the "Author" problem described by pampigt.  Use the "split" icon to display both the "Design" and "Code" modes, find "=@Author", and change it to "@Author" (there's only one occurrence).   Then save the page — the display will be refreshed.

Then I discovered a problem not described by "pampigt".  I tested the page at this stage, and found that in the browser, it displayed the "There are no posts in this category" message.  Examining the XSLT, it appeared to me that this meant that there were no rows returned by the query.  After much munching around, I found the key to this step:  Using the little arrow-head next to the web part, I opened the Common Data View Tasks menu, and selected "Filter:"  I knew I needed to filter on ID, so I selected that as the field name, and I selected "Equals" as the comparison.  Then, under "Value", I knew I needed value of the query string parameter ID, but that didn't appear in the dropdown.  However, "Create a new parameter..." did appear, so I selected it.  I gave the parameter name "ID" and the source as "Query String".  A few "OK"'s later, I saved the page and tested in the browser.  First Victory!

Now to get the attachments listed on the page.  For this, I used the code linked by "pampigt" from Dario Martirani <http://blogs.msdn.com/dmp/archive/2009/01/21/how-to-show-attachments-with-dataformwebpart.aspx>.  His Data View Web Part (he uses the older name "Data Form Web Part") is much simpler than the converted List View Web Part in Posts.aspx, but I took the XSL code for the dvt_1.rowview template and inserted it right above the "Posted at" part of the XSL for my Data View Web part.  I added a test so the code wouldn't be used if there were no attachments, and a <br /> at the end.  Save, test in browser — Second Victory!

Here's the XSL code I ended up with:

   <xsl:if test="@Attachments!='0'">
        Attachments:
           <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>
          <br />
</xsl:if>

Note that, contrary to Dario's post, this appears to work even though the "ListId" attribute is not set to the correct list.  I suppose that the "AttachmentsField" control is getting its context from somewhere other than its attributes...

As the last step, I opened Post.aspx in SPD and saved it as Post-dist.aspx (the version distributed), then put the test page into production by saving it as Post.aspx, so the new version is the target of all the right links.


Remaining issues:   
  1. adding attachment capabilities in the NewPost.aspx and EditPost.aspx pages (without modifying the 12 hive if possible)
  2. adding display of attachments to the blog home page


6 comments:

EconChris said...

Hi! Did you have any luck in getting an attachment button to appear when you post a new blog?

Dosh Garnet said...

Sorry, I haven't had time to try.

Rudy said...

Thank you SO MUCh for this, Dosh. I was wondering, do I have to do something to the code to automatically download the attachemtn once the link is clicked?

Dosh Garnet said...

@Rudy, sorry again, but I can't really answer. The subject site has long ago (a year?!) gone to the bit bucket, killed by budget cuts and reorganization, so I can't even check. I was under the impression that the links _were_ hot -- try it with some benign filetype like ".txt" to be sure there isn't some "protection" SharePoint feature in your way.

Waseem Khan- SharePoint Technology Consultant said...

Thank you very much, it works for me like a charm. On the very first attempt I got success.

Best Regards,
Waseem Khan

Dosh Garnet said...

Just to be clear: this work was done on SharePoint 2007 -- I don't know if it will still work on newer versions, or if there is some built-in functionality that makes all this unnecessary, because I don't work with SharePoint any more (yes, Virginia, there IS life after SharePoint, at least for some of us *grin*.