Select to view content in your preferred language

Display Latest Feature In a Feature Service

5474
5
05-07-2020 05:14 PM

Display Latest Feature In a Feature Service

When working with feature services, there may be duplicate features, but with different attributes.  Take for example a Survey123 form that reports the shelter's current population.  Multiple submissions for the same shelter can be provided throughout the day, but you would only like to display the most recent feature.  One way to achieve this is through the top filter parameter that's part of the Query Top Features operation.  This can be applied to an ArcGIS Online hosted feature layer view.  Below are the steps on how to do this.

1.  In ArcGIS Online, navigate to your hosted feature service Item Details and click Create View Layer:

2.  Specify a Name, Tags, and Description for the view:

In this example, you can see the Shelters have multiple entries for the same shelter, but with different Count values. 

3.  Go to the Item Details of the hosted view layer.  Scroll down and click View next to the URL:

4.  Click on the layer name under Layers

5.  Edit the URL and add admin between rest/services:

6.  Scroll down to the bottom of the page and click Update Definition

7.  Delete all the JSON in the Update Layer Definition dialog box

8.  Navigate to https://jsonlint.com/ to help define the JSON object to be passed

9.  There are 3 parameters that will be passed in the JSON object:

  • orderByFields:  the name of the field that contains the time you want to sort.  ASC = Ascending, DESC = Descending
  • groupByFields:  the name of the field to be incorporated in the grouping
  • topCount:  defines the number of features returned from the top features query and is a numeric value

 In this example, I want to show each shelter by the latest edit date, so I will enter the following (note:  enter the field names and not the field aliases):

{
	"viewLayerDefinition": {
		"topFilter": {
			"orderByFields": "last_edited_date DESC",
			"groupByFields": "sheltername",
			"topCount": 1
		}
	}
}‍‍‍‍‍‍‍‍‍

Enter this into JSONLint and validate it is correct.  Then paste this into the Update Layer Definition dialog box:

10.  Click Update Layer Definition

The hosted view layer will now show only the most recent feature for each shelter:

Comments
Nat_Carter
New Contributor

Is this applicable for Enterprise v10.9.1?  I have successfully applied the topFilter definition to the "viewLayerDefinition" but the view layer is reporting a Data Error.  The other properties in the JSON for the hosted feature view layer of "viewDefinitionQuery" and "definitionExpression" are null.  It looks like the "viewDefinitionQuery" property is what gets affected by the Set View Definition tool in the view layer's Visualization tab but the topFilter syntax can't be applied there.

Thanks for the help!  

FarmersBranchBrian
Occasional Contributor

For the purposes of documenting my experience, I have used this technique several times in ArcGIS Online with great success.

However, I have since encountered several people, myself included that have found it does not work on ArcGIS Enterprise Hosted Feature Layers (those created and stored in the Data Store).  There appears to be an underlying database issue for which I have yet to discover a workaround.  While there are other workarounds involving an Enterprise Geodatabase referenced Map or Feature Services, I have yet to find one for Hosted Feature Layers.

I have personally tested in 10.9.1 and witnessed others that have tested in 11.1 and 11.3 who all experience the same issue.  "Data Error" appears in the service's Data tab and no data is drawn in a map view.

MarkBockenhauer
Esri Regular Contributor

Slight update to above description.   For ArcGIS online at Step 4 you can click the Admin link on the service directories page.

MarkBockenhauer_0-1746630762498.png

Then continue with updating the layer definition.

MarkBockenhauer
Esri Regular Contributor

Oh and some other things to note...  if you copy the json from this post.. it adds extra characters that will cause an error.  (maybe that is what the JSONLint is about.)

 
 

MarkBockenhauer_2-1746631371342.png

also, the input dialog defaults to HTML, set it to JSON.

MarkBockenhauer_3-1746631501723.png

 

 

Mike_Nordquist
Occasional Contributor

@MarkBockenhauer 
Hi Mark,
I had been using this successfully for an ArcGIS Online Hosted Feature Layer View, but recently it has stopped updating when data is added or edited.
If I go into the updateDefinition and click run, then it refreshes the data.
There are times where it seems to have the data in the table, but won't be available in Experience Builder or Dashboards.
Any thoughts on this?

Version history
Last update:
‎12-12-2021 03:35 AM
Updated by: