How to obtain feature ID

Document created by djohnson2k7 on Mar 22, 2015
Version 1Show Document
  • View in full screen mode
Is there a sample around that shows how to obtain the feature ID (place it into a variable) when the user clicks on a point feature?
Silverlight 4

Thanks
Kindly refer to this documentation: http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.FeatureService.FeatureLayerInfo~ObjectIdField.html

If LayerInfo is retrieved after the layer has initialized so be sure to check that layer.LayerInfo is not null.

The following code retrieves the feature ID:
var id = graphic.Attributes[layer.LayerInfo.ObjectIdField];

Kindly refer to this documentation: http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.FeatureService.FeatureLayerInfo~ObjectIdField.html

If LayerInfo is retrieved after the layer has initialized so be sure to check that layer.LayerInfo is not null.

The following code retrieves the feature ID:
var id = graphic.Attributes[layer.LayerInfo.ObjectIdField];


Thanks Jenn. I'm afraid that as a beginner I need a little more help than that provides. Working in VB the line
Public ReadOnly Property ObjectIdField As String
 
doesn't tell me anything about how to make the assignment. Something like this is needed.

myVariable = ????
messagebox.show("myVarable = " + myVariable.tostring)

Allso it tells me nothing about the requirements of the service (if there are any). My features are shapefile based.

Thanks
Oh, the code snippet and documentation I provided is for FeatureLayer, where ObjectIDField will be known after the layer is Initialized and LayerInfo is retrieved.

How are you saving the graphic to your database? How is the object ID created? Do you save the object id as part of the attributes? If yes, then however you named this field, is also the key you used in getting the attribute value.

Same code snippet as my previous post, except you replace the key "layer.LayerInfo.ObjectIdField" with your own name for your object id.
Hmmm. . .
I'm not sure I can answer those questions. (Still a very basic beginner here.)
The layer is based on a shapefile ( we absolutely are not ready to deal with geodatabase yet).
The purpose of the project is to allow the user to select a point and edit some (but not all) of the data associated with the point. The data will not be in the attribute table, but will instead be in a related child table (1 to many relationship). So, my thought was to simply obtain the FID of the point and then programmatically construct a query to the related table that will display the records to be edited in some sort of datagrid. The fields to be displayed and edited will be dynamic, hence the need to control it with code rather than a standard tool.

The application is basically a volunteer signup form where users will signup to monitor an intersection for one of the available timeslots during the day. Once a timeslot has been taken it will not be available to other users.

This is my first attempt to work with data in Silverlight so even that is a daunting challenge. Have you a sample that works anything like this?
I'm sorry I have more questions than answers. I wanted to know how you load shapefiles to your map?  Does it have a corresponding .dbf that holds the attributes of the feature?  I imagine that this is where you'd keep record of the object ID. 

How you create and save the object ID is important to know because it also dictates how you can retrieve them. You can use the graphic's Attributes to hold this information (http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Graphic~Attributes.html).

It says that this is read-only property meaning you cannot create a new instance, but you can update the existing instance. You can do something like:
Graphic g = new Graphic()
{
// set Geometry property from ShapeFile.
}
var objectId; // retrieve from DBF file (?)
g.Attributes["objectid"] = objectId; // to save object id onto an attribute field
var oid = g.Attributes["objectid']; // to retrieve the object id from an attribute field.

Note that in this example, I just used "objectid" as my key for the field. You can name this as you wish but be mindful that the key you used to save is the same key you need to use to retrieve.

Shapefile is actually an unfamiliar territory for me but I found some links that might be useful for your project.
http://esrislcontrib.codeplex.com/
http://forums.arcgis.com/threads/6425-Export-to-shapefile-other-format
http://forums.arcgis.com/threads/13680-Download-Shapefiles-Similar-to-the-Extract-Service-in-ArcIMS

Other developers are welcome to provide additional help :)
I'm sorry I have more questions than answers. I wanted to know how you load shapefiles to your map?  Does it have a corresponding .dbf that holds the attributes of the feature?  I imagine that this is where you'd keep record of the object ID. 

How you create and save the object ID is important to know because it also dictates how you can retrieve them. You can use the graphic's Attributes to hold this information (http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Graphic~Attributes.html).

It says that this is read-only property meaning you cannot create a new instance, but you can update the existing instance. You can do something like:
Graphic g = new Graphic()
{
// set Geometry property from ShapeFile.
}
var objectId; // retrieve from DBF file (?)
g.Attributes["objectid"] = objectId; // to save object id onto an attribute field
var oid = g.Attributes["objectid']; // to retrieve the object id from an attribute field.

Note that in this example, I just used "objectid" as my key for the field. You can name this as you wish but be mindful that the key you used to save is the same key you need to use to retrieve.

Shapefile is actually an unfamiliar territory for me but I found some links that might be useful for your project.
http://esrislcontrib.codeplex.com/
http://forums.arcgis.com/threads/6425-Export-to-shapefile-other-format
http://forums.arcgis.com/threads/13680-Download-Shapefiles-Similar-to-the-Extract-Service-in-ArcIMS

Other developers are welcome to provide additional help :)


Jenn - I'm just getting back to this. The "shapefile" is actually a group of files that contain the feature geometry, projection, metadata, and other info. The attributes are contained in a traditional *.dbf file with a field 'FID' (FeatureID) as the unique identifier that links the feature to the attributes. The shapefile or layer is placed on the map via the service with code like this:
   <!--<esri:Map x:Name="MyMap" Grid.RowSpan="2" >
    <esri:Map.Layers>
     <esri:ArcGISDynamicMapServiceLayer ID="Intersections" 
      Url="http://198.182.104.173/ArcGIS/rest/services/Intersection_Test/MapServer" />
    </esri:Map.Layers>
   </esri:Map> -->
In this case its a point layer. What I want to do is click a point and capture the FID for that point so I can use it to query a separate but related table.
Does that help?
Thanks
Just to be clear: The Client APIs does not support working with Shapefiles. You will need to publish your data to ArcGIS Server first. After that, it doesn't' really matter how the data is stored as long as it is server out using ArcGIS Server. When you are at that point, you will be using a FeatureLayer as Jennifer was getting at.
Just to be clear: The Client APIs does not support working with Shapefiles. You will need to publish your data to ArcGIS Server first. After that, it doesn't' really matter how the data is stored as long as it is server out using ArcGIS Server. When you are at that point, you will be using a FeatureLayer as Jennifer was getting at.


Morton -
Yes, I understand that the map needs to be delivered as a service from the server. That is what I have. (See the markup in my previous post.) What I don't understand (very much a beginner) is how or where to go from here. I can click the map point and obtain the datagrid but how can I pick out the FID from it? (and preferrably not display it)
Thanks
Since you already have a map service, can you use FeatureLayer instead? http://help.arcgis.com/en/webapi/silverlight/help/Creating_featurelayer.htm

There is a MouseLeftButtonDown you can subscribe to, on the Feature Layer itself, this has GraphicMouseButtonEventArgs and you can easily use e.Graphic to identify the feature that was clicked.
Thanks Jenn -
My layer displays OK as a MapDynamicServiceLayer but doesn't display as a featureService layer. Apparently the featureService layer requires the data to be taken from a geodatabase?

I guess I am puzzled because I can Identify and see the data I want, or I can load a featuredataform as if I was going to edit (but I cannot edit) and also see the data I need, why can't I just parse that data into a variable? Seems like this would be a fairly basic thing to do without requireing the jump into geodatabases.
:)
If you are using FeatureDataForm, then you must be using FeatureLayer, because FeatureDataForm need to set its FeatureLayer property in order to display attributes.
If you are using FeatureDataForm, then you must be using FeatureLayer, because FeatureDataForm need to set its FeatureLayer property in order to display attributes.


Actually I'm not using FeatureDataForm. I was just blindly copying the ESRI sample and combining the featureDataForm with a DynamicMapServiceLayer. btw - It does work to bring up the data but doesn't allow editing.
I think you will still get compile error because the FeatureLayer property would expect only FeatureLayer type. It is however, possible to create a FeatureLayer from a map service (maybe this is what you were doing). It is true that FeatureDataForm will show read-only fields if the FeatureLayer comes from a map service. You will not be allowed to edit, but you will be allowed to retrieve the feature ID, if this is part of your feature Attributes.
I think you will still get compile error because the FeatureLayer property would expect only FeatureLayer type. It is however, possible to create a FeatureLayer from a map service (maybe this is what you were doing). It is true that FeatureDataForm will show read-only fields if the FeatureLayer comes from a map service. You will not be allowed to edit, but you will be allowed to retrieve the feature ID, if this is part of your feature Attributes.


Well . . . You were right. I went back to my previous attempt and it was in fact a FeatureLayer that I was playing with. Here is the code
<esri:FeatureLayer ID="PointLayer"
    Url="http://198.182.104.173/ArcGIS/rest/services/Intersection_Test/MapServer/0"
                MouseLeftButtonUp="FeatureLayer_MouseLeftButtonUp"
                DisableClientCaching="True" 
                Mode="OnDemand"
                SelectionColor="#FFFFFF00"
                OutFields="*" />


Interestingly, when I copy this exact code into my current project I get 2 errors that say "Mode" and "SelectionColor" are not properties of Feature Layer. The only difference is the current project is in Silverlight 4.

Also, the codebehind
private void FeatureLayer_MouseLeftButtonUp(object sender, GraphicMouseButtonEventArgs args)
tells me I may be missing a reference because GraphicMouseButtonEventArgs cannot be found. I have all the same references (I think) but I did convert from VB to C#. So, before I can go further I need to get these fixed.
For Silverlight 4, you need API v2.0 and up. Yeah, check that the project references are correct and that they point to the proper file location. Once you have verified that your project has the proper version and files, maybe you just need to add imports statement:
Imports ESRI.ArcGIS.Client

For Silverlight 4, you need API v2.0 and up. Yeah, check that the project references are correct and that they point to the proper file location. Once you have verified that your project has the proper version and files, maybe you just need to add imports statement:
Imports ESRI.ArcGIS.Client


Well, I think I got the right file.
ArcGISSilverlightWPF20.exe filesize 7193 kb

I reinstalled it anyway with no change in the result. Is there a post install of some kind needed to integrate it with VWD?
This is all you need for installing the API. http://help.arcgis.com/en/webapi/silverlight/help/Installation.htm
This is all you need for installing the API. http://help.arcgis.com/en/webapi/silverlight/help/Installation.htm


Thanks. That confirms that I had the right file. But somehow my references were pointing to ver 1.1. So I deleted them and rereferenced the correct ones. I now have a FeatureLayer with a MouseLeftButtonUp="FeatureLayer_MouseLeftButtonUp" event. Can you show me how to capture the attribute in this event?
You can do this by listening to the FeatureLayer_MouseLeftButtonUp event handler. Use the following code snippet to get the attribute collection of the Graphic object that has been clicked by the user:
IDictionary<string, object> attributes = e.Graphic.Attributes;

You can do this by listening to the FeatureLayer_MouseLeftButtonUp event handler. Use the following code snippet to get the attribute collection of the Graphic object that has been clicked by the user:
IDictionary<string, object> attributes = e.Graphic.Attributes;


TaDa!! It works. Thanks so much to both of you.
  private void FeatureLayer_MouseLeftButtonUp(object sender, GraphicMouseButtonEventArgs args)
  {
   FeatureLayer featureLayer = sender as FeatureLayer;

   for (int i = 0; i < featureLayer.SelectionCount; i++)
    featureLayer.SelectedGraphics.ToList()[i].UnSelect();

   args.Graphic.Select();
   IDictionary<string, object> attributes = args.Graphic.Attributes;

   MessageBox.Show("This FID = " + attributes["FID"]) ;

  }


Now, is there a way I can change the symbol for the selected and unselected points in the FeatureLayer?
Please take a look a the following SDK sample to get the whole idea:
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SelectGraphics
You would need to set the FeatureSymbol property of your FeatureLayer to your custom symbol.

Please take a look a the following SDK sample to get the whole idea:
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SelectGraphics
You would need to set the FeatureSymbol property of your FeatureLayer to your custom symbol.


The sample uses graphics, I am using a FeatureLayer. Meanwhile I found this information.
http://help.arcgis.com/en/webapi/silverlight/help/creating_featurelayer.htm
Using the info therein I coded my map to match their sample like this.
<Grid.Resources>
    <esriSymbols:SimpleMarkerSymbol x:Name="MyMarkerSymbol" Color="Red" Style="Diamond" Size="10" />
   </Grid.Resources>
  
   <esri:Map x:Name="MyMap" Grid.RowSpan="2" >
    <esri:Map.Layers>
     <esri:ArcGISDynamicMapServiceLayer ID="SchoolSearch3" 
      Url="http://198.182.104.173/ArcGIS/rest/services/SchoolSearch3/MapServer"       VisibleLayers="0,1"/>
     <esri:FeatureLayer ID="MyFeatureLayer"
      Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0" 
      MouseLeftButtonUp="FeatureLayer_MouseLeftButtonUp"
      DisableClientCaching="True"
      Mode="OnDemand"
      OutFields="*" 
      SelectionColor="Yellow"     
      FeatureSymbol="{StaticResource MyMarkerSymbol}" >

     </esri:FeatureLayer>
    </esri:Map.Layers>
   </esri:Map>
which seems to work as expected except that I don't get the yellow selected color. However, when I substitute MY url in place of the sample, (Url="http://198.182.104.173/ArcGIS/rest/services/Intersection_Test/MapServer/0") it does not work. So the conclusion is that I must need to do something to the service to make this work? Correct? and if so what?

Thanks
If you can't see your layer on the map you may be using an ArcGIS Server service over a scheme different than the host application, please make sure you have the clientaccesspolicy.xml on your services Web server. More info here: http://blogs.esri.com/Dev/blogs/silverlightwpf/archive/2009/08/31/Using-services-across-schemes.aspx

If the above is not the problem and you can see your layer then if your layer has a Renderer defined in its service you can replace it with your own Renderer and use FeatureLayer's Renderer property to override the Renderer used in your FeatureLayer, i.e.
<esri:FeatureLayer ID="MyFeatureLayer" Renderer="{StaticResource PointSymbol}" ... />

Where:
<Grid.Resources>
 <esri:MarkerSymbol x:Key="SelectMarkerSymbol" >
  <esri:MarkerSymbol.ControlTemplate>
   <ControlTemplate>
    <Ellipse x:Name="Element" Width="15" Height="15" StrokeThickness="10" 
       Stroke="Green" >
     <VisualStateManager.VisualStateGroups>
      <VisualStateGroup x:Name="SelectionStates">
       <VisualState x:Name="Unselected" />
       <VisualState x:Name="Selected">
        <Storyboard>
         <ColorAnimation Storyboard.TargetName="Element" 
          Storyboard.TargetProperty="(Ellipse.Stroke).(SolidColorBrush.Color)"
          To="Cyan" Duration="00:00:0.25"/>
        </Storyboard>
       </VisualState>
      </VisualStateGroup>
     </VisualStateManager.VisualStateGroups>
    </Ellipse>
   </ControlTemplate>
  </esri:MarkerSymbol.ControlTemplate>
 </esri:MarkerSymbol>
 <esri:SimpleRenderer x:Key="PointSymbol" Symbol="{StaticResource SelectMarkerSymbol}" />
</Grid.Resources>

Its not a problem with cross domain and yes I can see the layer in my map, I just can't get it to show with a different symbol. How would my service have been defined with a built in Renderer? I can modify the service if necessary to avoid additional coding to deal with it.
You can look at this help doc http://help.arcgis.com/en/arcgisserver/10.0/help/arcgis_server_dotnet_help/index.html#/Authoring_feature_services/009300000021000000/

There is a section on defining symbology.
You can look at this help doc http://help.arcgis.com/en/arcgisserver/10.0/help/arcgis_server_dotnet_help/index.html#/Authoring_feature_services/009300000021000000/

There is a section on defining symbology.


Do I infer correctly that to make use of FeatureSymbols the FeatureLayer must be based on a FeatureService and not a MapService?

Thanks
FeatureSymbol is used when the FeatureLayer (regardless if it comes from Feature Service or Map Service) does not already have a Renderer.

You can define the Renderer in the service itself or in the application. The help doc in my previous post describes how it's done on the service.

http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.GraphicsLayer~Renderer.html

Here's a sample how Renderer can be defined in your application: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#FeatureLayerRendering

This document was generated from the following discussion: How to obtain feature ID

Attachments

    Outcomes