I need to display attachments from a secure service in a popup, filtered by keyword. I can retrieve and filter the attachments using Arcade, but I can't seem to return the attachments to the popup. I'm hoping it's just a syntax issue, but I'm starting to wonder if this is even possible.
Here's my latest attempt:
var keyword = "my_keyword";
var atts = Attachments($feature);
var filteredAttachments = [];
for (var i = 0; i < Count(atts); i++) {
var attachment = atts[i];
if (attachment.keywords == keyword) {
Push(filteredAttachments, attachment);
}
}
Console(filteredAttachments)
return {
"attachment": filteredAttachments,
"type": "attachment",
"displayType": "auto"
}
This logs the following to the console, so everything is good up to the return statement.
[{"id":2,"name":"Discrepancies_image-20240321-155540-91b088a433494fb5aebd156de9c225e4-75a31dfa6c20461183d7004d680f0a78.jpg","contenttype":"image/jpeg","size":244565,"exifinfo":null,"keywords":"my_keyword"}]
I can't use the method of constructing the attachment URL because there's no way to provide a token. I've tried a dozen different iterations of the return statement.
Is it possible to filter attachments by keyword in Map Viewer popups?
Solved! Go to Solution.
I discovered a partial solution, which is to construct the attachment URLs and generate an API key to append to the URLs for access, then return as a mediaInfos element. API keys only last for up to one year, so I'm still curious if this is possible without a token or API key. Here's how I did it by constructing the attachment URLs and using an API key:
var keyword = "my_keyword"; // keyword to filter by
var atts = Attachments($feature);
var filteredAttachments = [];
var mediaInfos = []
var apiKey = YOUR_API_KEY_GOES_HERE
for (var i = 0; i < Count(atts); i++) {
var attachment = atts[i];
if (attachment.keywords == keyword) {
Push(filteredAttachments, attachment);
}
}
var baseUrl = "https://services9.arcgis.com/iERBXXD4hiy1L6en/ArcGIS/rest/services/Testing_Attachments/FeatureServer/0";
for (var j = 0; j < Count(filteredAttachments); j++) {
var attachment = filteredAttachments[j];
var attachmentUrl = baseUrl + "/" + $feature.OBJECTID + "/attachments/" + attachment.id + "?token=" + apiKey;
Push(mediaInfos, {
"type": "image",
"caption": "test",
"value": {"sourceURL": attachmentUrl}
}
)
}
return {
"mediaInfos": mediaInfos,
"type": "media"
};
I discovered a partial solution, which is to construct the attachment URLs and generate an API key to append to the URLs for access, then return as a mediaInfos element. API keys only last for up to one year, so I'm still curious if this is possible without a token or API key. Here's how I did it by constructing the attachment URLs and using an API key:
var keyword = "my_keyword"; // keyword to filter by
var atts = Attachments($feature);
var filteredAttachments = [];
var mediaInfos = []
var apiKey = YOUR_API_KEY_GOES_HERE
for (var i = 0; i < Count(atts); i++) {
var attachment = atts[i];
if (attachment.keywords == keyword) {
Push(filteredAttachments, attachment);
}
}
var baseUrl = "https://services9.arcgis.com/iERBXXD4hiy1L6en/ArcGIS/rest/services/Testing_Attachments/FeatureServer/0";
for (var j = 0; j < Count(filteredAttachments); j++) {
var attachment = filteredAttachments[j];
var attachmentUrl = baseUrl + "/" + $feature.OBJECTID + "/attachments/" + attachment.id + "?token=" + apiKey;
Push(mediaInfos, {
"type": "image",
"caption": "test",
"value": {"sourceURL": attachmentUrl}
}
)
}
return {
"mediaInfos": mediaInfos,
"type": "media"
};