if/else Statement
if/else Statement
Hi. I would like to show a 'View' hyperlink when a Sharepoint online attachment link is available from the JSON object returned. Using code below, im able to generate the list of attachments. However, it only displays one View hyperlink for cases with multiple attachments. Please advice.
I need to have a validation first to look for an attachment hyperlink and then create the view hyperlink for number of attachment links from JSON object.
$(document).ready(function() {
loadItems();
});
function loadItems() {
var oDataUrl = "SPO_Link/_api/web/lists/GetByTitle('List_Name')/items?$expand=AttachmentFiles";
$.ajax({
url: oDataUrl,
type: "GET",
dataType: "json",
headers: {
"accept": "application/json;odata=verbose"
},
success: mySuccHandler,
error: myErrHandler
});
}
function mySuccHandler(data) {
try {
$('#table_id').DataTable({
"dom": 'Bfrtip',
"aaData": data.d.results,
"aoColumns": [
{ "mData": "Title"},
{
"mData": "AttachmentFiles.results[].ServerRelativeUrl",
"render": function(data) {
if(data) {return data}
}
}
]
});
} catch (e) {alert(e.message);}
}
function myErrHandler(data, errMessage) {
alert("Error: " + errMessage);
}
This question has an accepted answers - jump to answer
Answers
Doing the following merges all values returned into one icon instead of one icon per attachment link. Reading further, i see that the attachment names are seperated with a , since the values returned from the JSON object are concatenated together.
Depending on whether its a an array of names or a string will determine the specific steps but you will need to use Javascript to iterate the names to generate a string that contains the output of each link. Maybe this pseudo code will help:
Kevin
worked perfectly thankyou. Pls note correction on line 4.