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
Awesome! it worked! Thank you so much!
ReplyDeleteWhere do I find the "List View Web Part"?
ReplyDeleteworks beautifully!!! thank you
ReplyDeleteIf the attachment is a picture can you show a thumbnail version instead of the file name?
ReplyDeleteThank you, thank you!!! I followed your instructions exactly and it worked perfectly!
ReplyDeletethank you very much!!! worked flawlessly.
ReplyDeleteThis 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!
ReplyDeleteJust 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?
ReplyDeleteFor those less than experts at Sharepoint (like me), here's how I found the List View Web Part, referenced in the instructions.
ReplyDeleteFrom 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.