Attachments Archives : Binary Bits https://blog.binarybits.net/tag/attachments/ Bits & Pieces - A blog by Kannan Balasubramanian Tue, 30 May 2017 10:22:39 +0000 en-GB hourly 1 https://wordpress.org/?v=6.5.2 Display attachments using JSLink in SharePoint https://blog.binarybits.net/display-attachments-using-jslink-in-sharepoint/ https://blog.binarybits.net/display-attachments-using-jslink-in-sharepoint/#respond Tue, 30 May 2017 10:20:58 +0000 https://blog.binarybits.net/?p=888 Recently I had a requirement where the customer wanted to show the actual attachment’s URL in the list view instead of default Icon. Following JSLink code will help to do that. (function () { var linkFiledContext = {}; linkFiledContext.Templates = {}; linkFiledContext.Templates.Fields = { "Attachments": { "View": OverrideAttachmentFieldRendering } }; SPClientTemplates.TemplateManager.RegisterTemplateOverrides(linkFiledContext); })(); function OverrideAttachmentFieldRendering(ctx) { […]

The post Display attachments using JSLink in SharePoint appeared first on Binary Bits.

]]>
Recently I had a requirement where the customer wanted to show the actual attachment’s URL in the list view instead of default Icon. Following JSLink code will help to do that.

(function () {
    var linkFiledContext = {};
    linkFiledContext.Templates = {};
    linkFiledContext.Templates.Fields = {
        "Attachments": { "View": OverrideAttachmentFieldRendering }
    };
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(linkFiledContext);
})();

function OverrideAttachmentFieldRendering(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 htmlString = "<span style='white-space: nowrap;'>No attachment(s)</span>";

    $.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++) {
                htmlString += "<a style='white-space: nowrap;'  href='" + data.d.results[i].ServerRelativeUrl + "'>" + data.d.results[i].FileName + "</a>";
                if (i != data.d.results.length - 1) {
                    htmlString += "<br/>";
                }
            }
        },
        error: function (error) {
            console.log("An error occured while fetching attachment details. Details: " + JSON.stringify(error))
        }
    });

    return htmlString;
}

Source: https://social.msdn.microsoft.com/Forums/office/en-US/29f7998f-ffc5-4877-b975-a49631b53ba2/show-attachments-with-jslink?forum=sharepointdevelopment

The post Display attachments using JSLink in SharePoint appeared first on Binary Bits.

]]>
https://blog.binarybits.net/display-attachments-using-jslink-in-sharepoint/feed/ 0