Get attachments from Feature Service

1592
6
06-24-2019 08:15 AM
MKa
by
Occasional Contributor III

I have this feature service that used the Global ID to like to another table.  I am able to get this table using a query and process the results.  However I cant figure out how to get the attachments for this feature.  The count always returns 0 even though the table hasAttachments property is true?  I am missing something.  I need to get and show the attachments (Photos) for a queried feature, but cant seem to get to them?

This function is a result of a table connection query, Feature status changed event.

    function processQueryFeaturesResult(queryResult) {
        try {
            if (!queryResult || !queryResult.iterator.hasNext) {
                // No features returned
                console.log("No features returned")
                return
            }

            while (queryResult.iterator.hasNext) {
                var feature = queryResult.iterator.next()
                if (feature.attributes){
                    var objectID = feature.attributes.attributeValue("OBJECTID");
                    var GlobalID = String(feature.attributes.attributeValue("GlobalID"));

		    //THESE values are indeed populated
                    console.log("EdgeMapPageFieldSelected - processQueryFeaturesResult - objectID", objectID);
                    console.log("EdgeMapPageFieldSelected - processQueryFeaturesResult - GlobalID", GlobalID);

	            //THIS Results in - feature.attachments QmlAttachmentListModel(0x1ee425912e0)
                    console.log("EdgeMapPageFieldSelected - processQueryFeaturesResult - feature.attachments", feature.attachments);
                    
	            //THIS Results in feature.attachments.count 0 
		    console.log("EdgeMapPageFieldSelected - processQueryFeaturesResult - feature.attachments.count", feature.attachments.count);

                }
            }

            //The table that these results are queryied from HAS Attachmenst
            console.log("EdgeMapPageFieldSelected - processQueryFeaturesResult - onlineAttachmentsTable.hasAttachments :", onlineAttachmentsTable.hasAttachments);

}
0 Kudos
6 Replies
LucasDanzinger
Esri Frequent Contributor

The issue is probably that you need to load the Feature before you access its attachments. GlobalID and ObjectID and any fields that are required for rendering will be there automatically but anything else can only be accessed once the object is loaded. More details on Loadable can be found here - https://developers.arcgis.com/qt/latest/qml/guide/loadable-pattern.htm

For example, something like the following should get you in the right direction:

// create array to store features in temporarily so they don't get garbage collected
property var featureList: []

function processQueryFeaturesResult(queryResult) {
        try {
            if (!queryResult || !queryResult.iterator.hasNext) {
                // No features returned
                console.log("No features returned")
                return
            }

            while (queryResult.iterator.hasNext) {
                var feature = queryResult.iterator.next()

                // add feature to array to avoid GC
                featureList.push(feature);

                // connect to loadStatusChanged signal
                feature.loadStatusChanged.connect(function(){
                    if (feature.loadStatus === Enums.LoadStatusLoaded) {
                         // access attachments once the feature is loaded
                    }
                });

                // load the feature
                feature.load();
            }
      }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
MKa
by
Occasional Contributor III

I will give this a shot online.  But it seems I have a bigger problem in that when i attempt to include this layer in my offline map package the, package somehow fails.  Is there also a trick to getting this attachments layer to be included in the offline package along with my other layers?

0 Kudos
MKa
by
Occasional Contributor III

I either need to figure out how to take this Attachments layer offline, or remove it from my offline download package.  I would like to take that layer offline, but over the past day I have ran into problems going offline with it.  Is there a way to remove a layer before the download offline package is made, exclued it in code or on my webmap in some way?

0 Kudos
LucasDanzinger
Esri Frequent Contributor

Can you try the following please:

- go to the web map item details page 

- Settings

- Offline > Advanced Options

- Make sure you have the radio button checked that says "Device will download and get updates to features and attachments."

details here - https://doc.arcgis.com/en/arcgis-online/manage-data/take-maps-offline.htm#ESRI_SECTION1_6CE94B6639C4...

0 Kudos
MKa
by
Occasional Contributor III

I am using your code from above, but can never get my loadstatuschanged signal to trigger?  Am I missing something.  I have pretty much used the exact same code you have above, but don't ever see anything happen in the loadchanged area.  That is were I want to collect the attachments that should be returning.  I can confirm that the queryresult does iterate, but never hits that signal.  What am i missing?

property var featureList: []
    // Process query results the same regardless of online / offline
    function processQueryFeaturesResult(queryResult) {
        console.log(" - processQueryFeaturesResult - Entered");
        
        if (!queryResult || !queryResult.iterator.hasNext) {
            // No features returned
            console.log("No features returned")
            return
        }
        
        while (queryResult.iterator.hasNext) {
            var feature = queryResult.iterator.next()
            
            // add feature to array to avoid GC
            featureList.push(feature);
            
            var objectID = feature.attributes.attributeValue("OBJECTID");
            var surveyGlobalID = String(feature.attributes.attributeValue("SurveyGlobalID"));
            
            console.log(" - processQueryFeaturesResult - objectID - ", objectID);
            console.log(" - processQueryFeaturesResult - surveyGlobalID - ", surveyGlobalID);
            
            // connect to loadStatusChanged signal
            //THIS NEVER SEEMS TO TRIGGER AND SHOW IN THE CONSOLE
            feature.loadStatusChanged.connect(function(){
                console.log(" - processQueryFeaturesResult - feature.loadStatus - ", feature.loadStatus);
                
                if (feature.loadStatus === Enums.LoadStatusLoaded) {
                    console.log(" - processQueryFeaturesResult - Enums.LoadStatusLoaded - ", feature.loadStatus);
                }
            });
            
            // load the feature
            feature.load();
            
            
        }
        
        return
    }
0 Kudos
MKa
by
Occasional Contributor III

This seems to at least get me the attachements.  Doesn't seem to work offline however?  I now need to display this attachment to the screen, but that should be easy if I put it in my display model.  But I need to do this offline too, or remove this layer from my offline Web Map somehow.

 while (queryResult.iterator.hasNext) {
                var selectedFeature = queryResult.iterator.next()

                var attachmentListModel = selectedFeature.attachments;
                attachmentListModel.fetchAttachmentsStatusChanged.connect(function() {
                    if(attachmentListModel.fetchAttachmentsStatus === Enums.TaskStatusCompleted){
                        console.log("fetchAttachmentsStatus - Loaded");
                        console.log(attachmentListModel.count);
                        attachmentListModel.forEach(function(element, index, array) {
                            element.fetchDataStatusChanged.connect(function(){
                                if(element.fetchDataStatus===Enums.TaskStatusCompleted){
                                    console.log("fetchAttachmentsStatus - element.attachmentUrl - ", element.attachmentUrl);

                                    console.log(element.attachmentUrl);
                                }
                            });
                            element.fetchData();
                        });
                    }
                });
0 Kudos