Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
(function () {
var attachmentsFiledContext = {};
attachmentsFiledContext.Templates = {};
attachmentsFiledContext.Templates.Fields = {
"Attachments": { "View": AttachmentsFiledTemplate }
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(attachmentsFiledContext);
})();
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 = "";
$.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) {
}
});
return str;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.