[Forum FAQ] Show Attachments in SharePoint 2013 Custom List View

  • Introduction:

    By default, there is an Attachments column in the SharePoint List, some people want to display attachments name and click name can open the documents in List View. In this article, we would show you the method with REST API, JSLink and jQuery.

    Solution:

    The steps in detail as follows:

    1.        Download the jQuery API  and upload the js file into the SiteAssets Document Library.
    2.        Save the following code as a js file (showAttachments.js) and upload it into the SiteAssets Document Library.
    (function () {
    
        // Create object that have the context information about the field that we want to change it output render  
    
        var attachmentsFiledContext = {};
    
        attachmentsFiledContext.Templates = {};
    
        attachmentsFiledContext.Templates.Fields = {
             
            "Attachments": { "View": AttachmentsFiledTemplate }
        };
    
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides(attachmentsFiledContext);
    
    })();
    
    
    // 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);
    }
    
    //get attachments field properties
    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;
    }


      3.     Edit the list view page.

      4.Edit the list web part. Go to Miscellaneous -> JS Link.

      5.    Add the following URL into the JS Link textbox.

      ~site/SiteAssets/jquery-1.11.1.min.js|~site/SiteAssets/showAttachments.js

      Result:<o:p></o:p>


      Please click to vote if the post helps you. This can be beneficial to other community members reading the thread.

    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值