关闭

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

541人阅读 评论(0) 收藏 举报
分类:
  • 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.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:262407次
    • 积分:3348
    • 等级:
    • 排名:第10407名
    • 原创:44篇
    • 转载:246篇
    • 译文:1篇
    • 评论:15条
    文章分类
    最新评论