Java script for downloading an attachment in a based on seach used in display template.
getAttachements: function (siteUrl, listId, itemId) {
console.log("siteUrl " + siteUrl);
console.log("listId " + listId);
console.log("itemId " + itemId);
doSpAction(function() {
console.log("doing sp action");
var clientContext = new SP.ClientContext(siteUrl);
// var list = cx.get_web().get_lists().getByTitle("Bestillinger");
var productList = clientContext.get_web().get_lists().getById(listId);
var targetItem = productList.getItemById(itemId);
clientContext.load(targetItem, "Title");
/* cx.executeQueryAsync(
Function.createDelegate(this, successHandler),
Function.createDelegate(this, errorHandler)
); */
clientContext.executeQueryAsync(
Function.createDelegate(this, listLoaded),
Function.createDelegate(this, listFailedToLoad)
);
function listLoaded() {
var allAtt = targetItem.get_attachmentFiles();
clientContext.load(allAtt);
clientContext.executeQueryAsync(
function () {
handleAtta(allAtt);
},
Function.createDelegate(this, listFailedToLoad)
);
//console.log(allAtt);
};
function listFailedToLoad() {
console.log(
"Request failed: " + arguments[1].get_message());
};
function handleAtta(allAtt) {
console.log(allAtt);
var enumerator = allAtt.getEnumerator();
while (enumerator.moveNext()) {
var current = enumerator.get_current();
var path = current.get_serverRelativeUrl();
var fileName = current.get_fileName();
$("#"+ itemId +"attlink").attr("href", path);
console.log(current);
console.log(path);
console.log(fileName);
};
};
});
}
getAttachements: function (siteUrl, listId, itemId) {
console.log("siteUrl " + siteUrl);
console.log("listId " + listId);
console.log("itemId " + itemId);
doSpAction(function() {
console.log("doing sp action");
var clientContext = new SP.ClientContext(siteUrl);
// var list = cx.get_web().get_lists().getByTitle("Bestillinger");
var productList = clientContext.get_web().get_lists().getById(listId);
var targetItem = productList.getItemById(itemId);
clientContext.load(targetItem, "Title");
/* cx.executeQueryAsync(
Function.createDelegate(this, successHandler),
Function.createDelegate(this, errorHandler)
); */
clientContext.executeQueryAsync(
Function.createDelegate(this, listLoaded),
Function.createDelegate(this, listFailedToLoad)
);
function listLoaded() {
var allAtt = targetItem.get_attachmentFiles();
clientContext.load(allAtt);
clientContext.executeQueryAsync(
function () {
handleAtta(allAtt);
},
Function.createDelegate(this, listFailedToLoad)
);
//console.log(allAtt);
};
function listFailedToLoad() {
console.log(
"Request failed: " + arguments[1].get_message());
};
function handleAtta(allAtt) {
console.log(allAtt);
var enumerator = allAtt.getEnumerator();
while (enumerator.moveNext()) {
var current = enumerator.get_current();
var path = current.get_serverRelativeUrl();
var fileName = current.get_fileName();
$("#"+ itemId +"attlink").attr("href", path);
console.log(current);
console.log(path);
console.log(fileName);
};
};
});
}
Comments