Select to view content in your preferred language

get feature from objectid directly

1151
3
Jump to solution
12-12-2023 08:04 PM
Lazyman
Occasional Contributor

Hi, I cannot figure out how to get feature in a layer or layerview from objectid directly, some function like "layerView.getFeatureById(objectid)".  I store the objectids in array but I don't know how to get the features back later. Could you help me, thanks!

0 Kudos
1 Solution

Accepted Solutions
Sage_Wall
Esri Contributor

Hi @Lazyman ,

There is a method named queryFeatures() on many of the layer types where you can pass in a Query object defining a query with the object id you are looking for.

https://developers.arcgis.com/javascript/latest/api-reference/esri-rest-support-Query.html

https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#queryFea...

You'll first need to define a Query and then pass that query as a parameter into queryFeatures().  One thing to look out for is the casing for the ObjectID field can be different depending on the environment hosting the service. So you might want to log out the layers fields or look at the REST enpoint of the service to match the casing for ObjectID. (OBJECTID, ObjectID, etc...)

const query = new Query();
query.where = "OBJECTID = 49"

layer.queryFeatures(query).then((results) => {
  console.log(results.features);
});

 

View solution in original post

3 Replies
Sage_Wall
Esri Contributor

Hi @Lazyman ,

There is a method named queryFeatures() on many of the layer types where you can pass in a Query object defining a query with the object id you are looking for.

https://developers.arcgis.com/javascript/latest/api-reference/esri-rest-support-Query.html

https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#queryFea...

You'll first need to define a Query and then pass that query as a parameter into queryFeatures().  One thing to look out for is the casing for the ObjectID field can be different depending on the environment hosting the service. So you might want to log out the layers fields or look at the REST enpoint of the service to match the casing for ObjectID. (OBJECTID, ObjectID, etc...)

const query = new Query();
query.where = "OBJECTID = 49"

layer.queryFeatures(query).then((results) => {
  console.log(results.features);
});

 

JoelBennett
MVP Regular Contributor

Everything said here is accurate, but the nuances of the OID field name can be avoided by using query.objectIds instead of query.where.

Lazyman
Occasional Contributor

Thank you both!

0 Kudos