Michael Vasilevsky
asked on
Open Attachment from within List Field in SharePoint Online
I'm using the below code to populate the list of attachments for each item in a SharePoint Online list. Works good but the hyperlink doesn't open the attachment, as is desired. Can I add an onClick event or similar to open the corresponding attachment in a new window?
(function () {
var fieldContext = {};
fieldContext.Templates = {};
fieldContext.Templates.Fields = {
"CommentAtt": { "View": attFieldTemplate },
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(fieldContext);
})();
function attFieldTemplate(ctx) {
var myComm = ctx.CurrentItem["ID"];
return getProjCommAttachments(myComm);
}
function getProjCommAttachments(myComm) {
var endpointUrl = "mySite/_api/web/lists/getbytitle('myist')/items(" + myComm + ")/AttachmentFiles";
var myAttachments = "";
getJson(endpointUrl)
.done(function(data) {
var items = data.d.results;
$.each(items, function(i) {
myAttachments += "<div><a href=" + data.d.results[i].ServerRelativeUrl + ">" + data.d.results[i].FileName + "</a></div>";
});
})
.fail(function(error) {
console.log(JSON.stringify(error));
});
return myAttachments;
}
function getJson(url) {
return $.ajax({
async: false,
url: url,
type: "GET",
contentType: "application/json;odata=verbose",
headers: {"Accept": "application/json;odata=verbose"}
});
} // getJson
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.