Display Links to SharePoint Item Attachments using CSR

I had an inquiry this week regarding whether or not CSR could be used within a SharePoint list view to display links directly to an item’s attachments. It took a bit of tweaking code around a bit, but Raymond Mitchell (@iwkid) and I were able to get it working.

Background: SharePoint lists may allow for attachments to list items. SharePoint also has a default column that can be used in a view for displaying whether or not an item *has* one or more attachments. (Is that Clippy in disguise?)

image

A user experience (UX) stumbling block however is that you have to open up an item view in order to see the list of items and links to them. If you’re familiar with SharePoint, not a huge UX issue, but still somewhat irritating compared to just seeing the list of items in the view directly. If you’re *not* SharePoint-savvy, then you might not know at all whether an attachment exists or how to see attachments.

We’re aiming for something like this:

image

The CSR JavaScript file can be found here: CSR_ViewAttach.js

Note: I’m sure the JavaScript gurus out there will find more code cleanup that can be done and methods that can be used to improve it – this should get you started. We’ll add new versions as improvements are made.

Instructions for use:

  1. If you don’t already have one, create a ‘scripts’ document library on the Office365 / SharePoint site you’ll be using the CSR file on.
  2. Download the JavaScript file from here
  3. Edit the JavaScript file to work with your column, list and site URLs (see more in the sample file or below)
  4. Upload the edited JavaScript file to your ‘scripts’ library.
  5. jQuery is required for this CSR to work. If you don’t have jQuery already, upload it to the same scripts library. (See this post for more information on using jQuery with CSR)
  6. Add the Script Editor web part to your page or use the ‘Embed Code’ option to add a reference to jQuery.
    Ex. “<script language=”javascript” type=”text/javascript” src=”../Scripts/jquery-2.1.3.min.js”></script>”
  7. Your list: If you don’t have a ‘spare’ text column you can use to override in a view, create a new text column (ex. ‘ViewAttach’).
  8. Create a view for your list that includes your new column. You can still use all the same sorting, filtering, and other other view configuration settings for this view.
  9. When using your list view, edit the web part and add the link to your JavaScript file to the JS Link property of the web part. It will look something like: ~site/Scripts/CSR_ViewAttach.js

If everything is in place, attachments in your list should now be listed in the column you identified. (If not, check the common troubleshooting items for CSR… hmm, that sounds like another post…)

Items to configure in the JavaScript/CSR file before it will work in your environment (these are also noted in the file itself):

  • The (internal) name of field being overridden. In the example code below we’re using the ‘Body’ column (which is the internal name for the ‘Description’ field) of a task list. You can override any text field and/or create your own.
  • The siteURL variable.  — See example in the CSR file itself
  • The listName variable.  — The list that includes the attachments you’re displaying links to (example in file)
  • The attachPath variable.  — Link to where attachments are stored (example in file)

Thanks, Notes, and References:

Please let me know how this works for you!

One comment

  1. Great solution!

    But there is a minor bug: When using SharePoint list filter options or paged results, the view will refresh and the listed attachments links will be duplicated.

    Solution: I just added a small PreRender function which simply resets the ‘requestIDs’ array (the function must be registered the same way as the PostRender function inside the overrideCtx part… ):

    function csrPreRender(ctx) {
    requestIDs = [];
    }

    Just wanted to post my findings, maybe it is helpful for someone…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.