You can search for features in one table that are related to features in another table, provided a relationship was defined between the two tables. Table relationships are defined using ArcGIS Desktop, as described in Essentials of relating tables in the ArcGIS Desktop help.
For example, say your app lets users view all the fire hydrants (fire hydrant features) in a city through a hydrants table. You now want users to be able to look up the inspection reports for a given hydrant that are stored in a different table, say hydrant-inspections. If a relationship has been defined between these tables, you can query the hydrants table to find related features from the hydrant-inspections table for a given hydrant.
The following steps describe how to set up the above scenario — searching for inspection reports (related features) of a given hydrant (feature). Note that you could also query the relationship in the other direction to search for all fire hydrants with a specified inspection criteria.
Search for related features
- Add the related tables to the same map. If the tables participating in the relationship are in a single map service, you can add the service to the map as an ArcGISMapImageLayer. This will add all spatial and non-spatial tables from the service. If a table is a spatial table from a map service or feature service, you can add it to the map's operational layers as a feature layer. Likewise, a non-spatial table can be added to the map's table collection. In a disconnected scenario, you add the tables to the map after downloading them into a mobile geodatabase.
All participating tables must be in the same map but they do not have to be loaded..
- If you want to return only those features from the table that participate in the provided relationship, set input parameters as described in this step. Otherwise, skip to step 4.
- Create a RelatedQueryParameters object.
- Provide a relationshipInfo from ArcGISFeatureLayerInfo. Any time you create a RelatedQueryParameters object, you must also provide a relationshipInfo that defines the relationship.
- You can provide additional input parameters, such as a whereClause, to further filter the related features, whether or not you wish to receive the geometry of the related features, and result sorting preferences.
- Query the table containing the feature for which you want related features. In the hydrant example, you would query the hydrants table to find inspection reports for a particular hydrant.
In a connected scenario, whether a query is online or local depends on the feature request mode of the related table. For more information about feature request modes, see the Table performance section in the Layers and tables topic.
- For On_Interaction_Cache, the query can be online or local as features are cached
- For On_Interaction_NoCache, the query is always online
- For Manual_Cache, the query is always local
- If you want to find related features from all participating relationships of a given feature, perform the search without any query parameters.
- Handle query results.
Query operations are asynchronous and emit signals with the query results or an error, if any. The results are an array of RelatedFeatureQueryResult objects, one for each related table that was queried. If more than one table was queried, multiple query operations may be performed internally and the query method is called back when all such operations have completed.
Each RelatedFeatureQueryResult contains a list of related features that can be iterated over. The query result also contains the relatedTable and relationshipInfo since multiple instances of the same table can be added to the map (for instance with different scale level visibility, definition expression, and so on). In such cases, a query is performed on each table instance and a RelatedFeatureQueryResult object is returned for each.
The query result also contains:
- The feature for which the related features were queried.
- Information on whether the resulting related feature count exceeded the maximum limit supported by the server.
See the sample List related features for a detailed code example.