Select to view content in your preferred language

How to get object id for the Query Layer

928
4
01-26-2024 11:42 AM
trushar
Emerging Contributor

Hello everyone, I am need of your assistance. 

I am unable to get object id for the Query Layer. I can get it for shapefile or sde feature class. 
Please take a look at the sample code below. 

Thank you,
Trushar Mistry

        //--------------------------------------------------
        // trusharm 2024-01-04 4:37pm, query layer GetDefinition() throws
        // exception so we are unable to get field names, hence we cannot
        // query layer at the moment until Esri fixes the issue
        [NotNull]
        private static string GetIdFieldForSpatialView([NotNull] FeatureLayer selectedFeatureLayer) =>
            ((CIMSqlQueryDataConnection)selectedFeatureLayer.GetDataConnection())
            .OIDFields
            .Contains("OBJECTID", StringComparison.InvariantCulture)
                ? "OBJECTID"
                : throw new InvalidOperationException("The view must at least have OBJECTID as unique index");

 

0 Kudos
4 Replies
GKmieliauskas
Esri Regular Contributor

I would recommend you to check selectedFeatureLayer for OID type field using code below:

 

var fieldDescriptions = selectedFeatureLayer.GetFieldDescriptions();
var oidField = fieldDescriptions.Find(x => x.Type == FieldType.OID);
if(oidField != null)
{
    var oidName = oidField.Name;
}

 

0 Kudos
trushar
Emerging Contributor

Thank you for your example @GKmieliauskas!

Apologies, I posted the question in rush and did not explain correctly nor did I copy the correct code sample.

Here is the core issue that I was attempting to get help on. I need access to column names and data under it so I can further bind it to my UI: 

using var table = (selectedFeatureLayer.GetDataConnection() is CIMSqlQueryDataConnection { WorkspaceFactory: WorkspaceFactory.SDE })
                    ? selectedFeatureLayer.GetTable()
                    : throw new InvalidOperationException("...");

var field in table.GetDefinition().GetFields()


The `GetDefinition()` fails for me for layer type `CIMSqlQueryDataConnection`. The exception is coming from `ArcGIS.Core.Data.GetDefinitionHandle()`. Is it possible there is an alternate way to get field names for query layers, would you happen to know?

Thank you again for your time,
Trushar M

0 Kudos
GKmieliauskas
Esri Regular Contributor

Could you please specify more information about ArcGIS Pro version and database type? There are some issues with GetDefinition method in ArcGIS Pro 3.2 and PostgreSql database. More info here.

0 Kudos
trushar
Emerging Contributor

Thank you for the link, I noticed DaveFullerton is running into same issue.
I am using ArcGIS Pro 3.2.1 and I am connected to SqlServer 2016.

Do I open a bug ticket with Esri?

Thanks,
Trushar

 

0 Kudos