Getting list of fields from identified feature

452
6
04-09-2010 12:57 AM
MarkSmith
New Contributor III
Hello,
Please help, I've been trying to return a list of field names from a feature when identified, I think it has something to do with fieldAliases but I can't find an example of it being used in code.  I can run through the features one by one and retrieve attributes by hard-coding a field name like so: val = idResult.feature.attributes['UNIQUE_ID'], but I want to write some generic code so I don't have to hard code the field names.
Below is the identify function which is looping through the identified features, can someone please post the few lines necessary to get the field names?  Thanks again, Mark.

function addToMap(idResults, evt) {
for (var i=0, il=idResults.length; i<il; i++) {
    var idResult = idResults;

    //Get attribute for a given field name
    val = idResult.feature.attributes['UNIQUE_ID']

    //Instead of above line, need to get list of field names for this feature so I can loop through them

    alert(val)
}
0 Kudos
6 Replies
PhilipSitton
New Contributor
I haven't tested this code so it may have typos/errors but I think the idea should be correct.

var i, fieldName, fieldValue, fieldNamesArr = [];

// This is how you can get the field names from an object.
for (fieldName in idResult.feature.attributes) {
    if (idResult.feature.attributes.hasOwnProperty(fieldName)) {
        fieldNamesArr.push(fieldName);
    }
}
// Now use the field names to retrieve attributes
for (i = 0; i < fieldNamesArr.length; i += 1) {
    fieldValue = idResult.feature.attributes[fieldNamesArr];
}
0 Kudos
BobBaker
New Contributor
Thanks. This code helped me.
0 Kudos
pavankalvala
New Contributor
Is there a way to populate field values from an identify dialog box to a doc template?

I created a doc template with few bookmarks in it. (Bookmarks are to populate the needed values from a source).

When I select a feature in ArcMap and identify the feature, we get an "identify dialog box" with "field name" to the left and "field values" to the right.

Is there a way to programmatically loop through this identify dialog box and populate field values to a MS word doc template.

Please guide me.
Thanks.
0 Kudos
pavankalvala
New Contributor
Hello,

I am a newbie to ArcObjects programming and I've been trying to return a list of field names and field values from a feature when identified.

Is there a way to programmatically loop through and get the field values from an identified feature?

Please help.
0 Kudos
derekswingley1
Frequent Contributor
Hi Pavan,

Are you using the JS API or ArcObjects? If it's ArcObjects, better to post in the AO forum:  http://forums.arcgis.com/forums/20-ArcObjects-All-Development-Languages
0 Kudos
pavankalvala
New Contributor
I have code below to pull field value from a feature selction onto a word document template. Instead of hard coding the field names, I would like to go through config file. How can this be done.

Here is the code below:

Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
      
        Dim pMxdoc As IMxDocument
        Dim pActiveView As IActiveView
        Dim pEnumLayer As IEnumLayer
        Dim pLayer As ILayer
        Dim pFlayer As IFeatureLayer
        Dim pFeatureSelection As IFeatureSelection
        Dim pSelectionSet As ISelectionSet
        Dim pEnumFeat As IEnumFeature
        Dim pEnumFeatSet As IEnumFeatureSetup
        Dim pFeat As IFeature
        Dim A As Double
        Dim B As String
        Dim B1 As String
      
        Dim pFeatSel As IFeatureSelection
        Dim pMap As IMap
        Dim pfeatureclass As IFeatureClass
        Dim fields As IFields
        Dim C As Long
        Dim ii As Long
        Dim Field As IField
        Dim intFldIndex As Integer
        Dim fieldname As String
        Dim fieldvalue As Integer
        Dim value As String
        Dim namelist As String
        Dim wdApp As Microsoft.Office.Interop.Word.Application
        Dim mydoc As Microsoft.Office.Interop.Word.Document
        Dim mywdRange As Microsoft.Office.Interop.Word.Range


        pMxdoc = m_application.Document
        pActiveView = pMxdoc.FocusMap
        pMap = pMxdoc.FocusMap
        pFlayer = pMxdoc.SelectedLayer
        pfeatureclass = pFlayer.FeatureClass
        fields = pfeatureclass.Fields
        pFeatureSelection = pFlayer
        pSelectionSet = pFeatureSelection.SelectionSet


       
        ' Verify that there is at least 1 layer in the TOC 
        If pMap.LayerCount < 1 Then
            MsgBox("Must have at least 1 layer in your map.")
            Exit Sub
        End If

        ' verify the name of the selected layer
        Dim count As Integer
        count = pSelectionSet.Count
        If pSelectionSet.Count <> 0 Then
            MsgBox("You have have selected:   " & pFlayer.Name)
        End If

        ' get the field count
        C = fields.FieldCount
        MsgBox("There are:   " & C & "fields in this layer")

        pEnumFeat = pMxdoc.FocusMap.FeatureSelection
        'pFeat = pEnumFeat.Next

        pEnumFeatSet = pEnumFeat
        pEnumFeatSet.AllFields = True
        pFeat = pEnumFeat.Next

        ' loop through each field and add the field name to a list
        For ii = 0 To fields.FieldCount - 1
            Field = fields.Field(ii)
            fieldname = Field.Name                  ' this gives the field name
            value = pFeat.Value(ii).ToString      ' this gives the field value
            namelist = namelist & fieldname & value & Chr(13)
        Next

        ' diaplay the list of field names in a messagebox
        MsgBox(namelist, , "field names and value")

    
        ' loop through to get value of FID
        Do While (Not pFeat Is Nothing)
  A = pFeat.Value(pFeat.Fields.FindField("UFO_ID"))    // @@@@ want to get this through config file
  B = pFeat.Value(pFeat.Fields.FindField("DATECREATE"))
          
            pFeat = pEnumFeat.Next
            'MsgBox(A)
        Loop

        ' opening word doc with the field values inside the word bookmarks
        wdApp = New Microsoft.Office.Interop.Word.Application
        With wdApp
            .Visible = True
            .WindowState = WdWindowState.wdWindowStateMaximize
        End With

        mydoc = wdApp.Documents.Add(Template:="C:\Documents and Settings\ZQ3079\Mobile project\ExportWord(VB)\Audit Form Final.doc")
        With mydoc.Bookmarks
            .Item("A").Range.InsertAfter(A)   ' "A" is bookmark name in word document
            .Item("C").Range.InsertAfter(B)   ' "C" is bookmark name in word document
     
        End With
    End Sub

Let me know.
0 Kudos