Wednesday, September 26, 2018

Show names of attachments from a SharePoint list item in a column


If you want to display the name of the attachment and click on the name to open the document in list view, here is a solution.


  • Enable the "Attachments" field in list view.



















  • Save the following code as a js file(showAttachments.js) and then upload the file into Site Assets document library.

(function () {
    (window.jQuery || document.write('<script src="//code.jquery.com/jquery-3.1.0.min.js"><\/script>'));
    //Create object that have the context information about the field that we want to change it output render 
    var linkFiledContext = {};
    linkFiledContext.Templates = {};
    linkFiledContext.Templates.Fields = {       
        "Attachments": { "View": AttachmentsFiledTemplate }
    };
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(linkFiledContext);
})();

// This function provides the rendering logic for list view
function AttachmentsFiledTemplate(ctx) {
    var itemId = ctx.CurrentItem.ID;
    var listName = ctx.ListTitle;      
    return getAttachments(listName,itemId);
}

function getAttachments(listName,itemId) {
 
    var url = _spPageContextInfo.webAbsoluteUrl;
    var requestUri = url + "/_api/web/lists/getbytitle('" + listName + "')/items(" + itemId + ")/AttachmentFiles";
    var str = "";
    // execute AJAX request
    $.ajax({
        url: requestUri,
        type: "GET",
        headers: { "ACCEPT": "application/json;odata=verbose" },
        async: false,
        success: function (data) {
            for (var i = 0; i < data.d.results.length; i++) {
                str += "<a href='" + data.d.results[i].ServerRelativeUrl + "'>" + data.d.results[i].FileName + "</a>";
                if (i != data.d.results.length - 1) {
                    str += "<br/>";
                }               
            }         
        },
        error: function (err) {
            //alert(err);
        }
    });
    return str;
}


  • Add the following reference in JSLINK in list view web part

~site/SiteAssets/showAttachments.js

























9 comments:

  1. Awesome! it worked! Thank you so much!

    ReplyDelete
  2. Where do I find the "List View Web Part"?

    ReplyDelete
  3. works beautifully!!! thank you

    ReplyDelete
  4. If the attachment is a picture can you show a thumbnail version instead of the file name?

    ReplyDelete
  5. Thank you, thank you!!! I followed your instructions exactly and it worked perfectly!

    ReplyDelete
  6. thank you very much!!! worked flawlessly.

    ReplyDelete
  7. This seems to only work if the list style is default. Is it possible to use for list views in other styles? My list style is boxed-no labels. Thanks!

    ReplyDelete
  8. Just came across this page. Where does one find the "List View Web Part" on my list? Does a web part need to be created first?

    ReplyDelete
  9. For those less than experts at Sharepoint (like me), here's how I found the List View Web Part, referenced in the instructions.

    From your list page:
    - Click "Edit" in the nav bar of your site
    - This pulls up the side bar to edit the order of pages (it has a + sign between page headers)
    - At the bottom, click the "Return to Classic Sharepoint"
    - Go to top right Settings option, and now you'll see "Edit Page"
    - From there, click the "Web Part" tab and the button "Web part Properties"
    You should now see the List View Web Part properties

    In my case, our site admin doesn't allow for pages to modify the JS Link area, so it doesn't show that field.



    ReplyDelete