Map service QueryRelatedRecords method

Queries and returns a related-record set of features or rows that are related to the specified features or rows for the specified layer or table ID.

QueryRelatedRecords(string MapName, int SourceTableID, FIDSet SourceFIDSet, RelateDescription)

Parameter

Description

MapName

The name of the map (data frame) that contains the layer associated with the SourceTableID parameter.

SourceTableID

The layer or standalone table id of the source layer or table.

SourceFIDSet

An FIDSet that contains an array of integers. Each integer represents a unique identifier for each feature or row (ObjectID) in the source layer or table whose related rows/features will be queried.

RelateDescription

Describes the related table for which the related records are being queried and how result records are formatted.

Return Value

A QueryResult object is returned. When "esriRelateResultRelatedRecordSetAsObject" is selected as RelateDescription's ResultFormat, the function returns RelatedRecordSet as the Object property (QueryResult.Object).

Remarks

The ID property of MapLayerInfo or StandaloneTableInfo is used as SourceTableID.

An FIDSet needs to be created and assign an array of ObjectIDs to the FIDArray property. Related records for ObjectID(s) contained by the FIDSet will be queried.

RelateDescription contains information about relationship that will be explored. It needs to be created and populated with relates ID, list of fields and output result format.

When more than one ObjectID is passed, use SourceRowID on the RelatedRecordSet.RelatedRecordGroup to relate 'related rows' back to the source feature or row.

The result honors the fields visibility set in the layer or the standalone table.

Relates with non GeoDatabase Tables

This method relies on IDs of features or rows from the source layer/table, when a table does not have an OIDField (not a Geodatabase table), finding related records from the source layer/table from the destination table is not possible. For example, when a Parcel layer is related to an ownership table (for example from an Oracle database), you may find related ownership information for a parcel, but the function does not allow to find parcels belong to a owner. In order to traverse the other way a table must have a OIDField. You may consider one of the following options for this type of scenario:

  • Import the table in your Geodatabase.
  • Use QuerLayer in ArcMap to add the table as a non-spatial table with user defined unique identifiers fields.
  • Create a unique, not null, indexed numeric field in your OLE DB table and the field will be recognized as a pseudo ObjectID field.

Output Spatial and Time Reference

If the desired spatial reference of the output geometry is different than the original one, RelateDescription.OutputSpatialReference should be used. Otherwise geometry is returned in the same spatial reference system as the MapServerInfo.DefaultMapDescription by default.

Values in StartTimeFieldName or EndTimeFieldName are returned in the Layer's TimeReference. If the desired time reference is different than the layer's one, OutputTimeReference should be explicitly set.

Examples

C#

//Layer A (Layer ID = 1) is related to Table B (StandaloneTable ID = 2) and you want to find all rows in

//Table B related to a feature in Layer A whose ObjectID is 3. In this case, you need to do the followings:

 

//Step 1: create a RelateDescription and populate information

RelateDescription pRD = new RelateDescription();

pRD.RelationshipID = 2;

pRD.RelatedTableFields = "*"; //or you can a pass a subset of fields

pRD.ResultFormat = esriRelateResultFormat.esriRelateResultRelatedRecordSetAsObject;

 

//Step 2: create a FIDSet

int[] intOIDs = new int[1] {3};

FIDSet pSrcFIDSet = new FIDSet();

pSrcFIDSet.FIDArray = intOIDs;

 

//Step 3: execute the function

QueryResult pQR = QueryRelatedRecords(aMapName, 1, pSrcFIDSet, pRD);

 

//Step 4: get result

RelatedRecordSet pRelRecordSet = pQR.Object;

 

//number of elements in RelatedRecordGroups matches with number of ObjectID passed in as SourceFIDSet

RelatedRecordGroup pRelRecGroup = pRelRecordSet.RelatedRecordGroups[0];

Console.WriteLine("Feature with ObjectID = " + pRelRecGroup.SourceRowID.toString()); //object id of the source feature

Console.WriteLine("has " + pRelRecGroup.Records.length.toString() + " related rows");

VB.NET

//Layer A (Layer ID = 1) is related to Table B (StandaloneTable ID = 2) and you want to find all rows in

//Table B related to a feature in Layer A whose ObjectID is 3. In this case, you need to do the followings:

//Step 1: create a RelateDescription and populate information

Dim pRD As RelateDescription = New RelateDescription()

pRD.RelationshipID = 2

pRD.RelatedTableFields = "*" 'or you can a pass a subset of fields

pRD.ResultFormat = esriRelateResultFormat.esriRelateResultRelatedRecordSetAsObject

 

'Step 2: create a FIDSet

Dim intOIDs() As Integer = New Integer() {3}

Dim pSrcFIDSet As FIDSet = New FIDSet()

pSrcFIDSet.FIDArray = intOIDs

 

'Step 3: execute the function

Dim pQR As QueryResult = mapservice.QueryRelatedRecords("mapName", 1, pSrcFIDSet, pRD)

 

'Step 4: get result

Dim pRelRecordSet As RelatedRecordSet = pQR.Object

'number of elements in RelatedRecordGroups matches with number of ObjectID passed

'in as SourceFIDSet

Dim pRelRecGroup As RelatedRecordGroup = pRelRecordSet.RelatedRecordGroups(0)

Console.WriteLine("Feature with ObjectID = " + pRelRecGroup.SourceRowID.ToString())

Console.WriteLine("has " + pRelRecGroup.Records.Length.ToString() + " related rows")

2/28/2020