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?)
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:
Instructions for use:
- 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.
- 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)
- Add the Script Editor web part to your page or use the ‘Embed Code’ option to add a reference to jQuery.
- 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’).
- 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.
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…)
- 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:
- Thanks to Raymond Mitchell (@iwkid) for his help in getting the code to work.
- Using jQuery for CSR
- JS Link – Hello World
Please let me know how this works for you!