This tutorial demonstrates how to build data editing workflows with your add-in, updating feature attribute data using the Inspector
class.
With the ArcGIS Pro SDK for .NET, you can extend ArcGIS Pro with your own unique tools and workflows. Using Microsoft Visual Studio and the Pro SDK, developers can build Pro add-ins and solution configurations that provide users with custom functionality specific to their organization or industry.
The ArcGIS Pro SDK provides an extensive Editing API that supports different data editing patterns based on your workflow. One of these patterns includes use of the Inspector
class for getting and setting feature attribute values.
In this tutorial, you will create an add-in that updates the attributes of selected features from a feature class you specify within your project that is based on an attribute name and a value that you provide in custom edit boxes.
Prerequisites
- Microsoft Visual Studio is required. See the ArcGIS Pro system requirements for latest version and compatibility.
- Install ArcGIS Pro version 3.3 or higher.
- Install the ArcGIS Pro SDK for .NET. See the Installation ProGuide for more information.
- This tutorial uses Pro SDK Community sample data.
Steps
Create a new ArcGIS Pro Add-In Visual Studio Project
-
Start Visual Studio.
-
Choose File > New > Project and then from the ArcGIS templates group, select ArcGIS Pro Module Add-in. Name the add-in project
Edit
.With Inspector By default, the
Config.daml
file opens in Visual Studio. TheModule1.cs
file contains add-in module code.Note also in the
Config.daml
file that theinsert
tag'sModule id
attribute matches the ID in theModule1.cs
file. Further, that theclass
attribute also matches the class name of the module.Name The ArcGIS templates group is located in Templates > Visual C# > ArcGIS. Confirm that the latest .NET Framework is selected.
Add Ribbon controls to your add-in
You will now add three controls to your add-in to be used on the ribbon.
-
Right-click the project and choose Add > New Item and then select the Visual C# empty class definition from the list of item templates.
-
When prompted, name the new class file
Attribute
and then click Add to close the dialog box.Name To Use Edit Box.cs -
Repeat this action again to add another class definition file. This time name the new class file
Mod
.Value To Set Edit Box.cs -
Right-click the project a third time and click Add > New Item and then from the ArcGIS Pro Add-ins group, select ArcGIS Pro button from the list of item templates.
-
When prompted, name the new class file
Update
and then click Add to close the dialog box. The ArcGIS Pro Button template provides the basic contents of the class file, as well as corresponding code in theValues Button.cs Config.daml
file for the ArcGIS Pro UI.
Update code for the controls
-
Open the
Attribute
file and replace the contents of the class with the following:Name To Use Edit Box.cs This code sets the class to a Pro EditBox and the constructor sets and references an instance of the edit box in
Module1
(added in the next step). The code also sets the edit box text to an empty string.Use dark colors for code blocks Copy class AttributeNameToUseEditBox : ArcGIS.Desktop.Framework.Contracts.EditBox { public AttributeNameToUseEditBox() { Module1.Current.AttributeNameToUseEditBox1 = this; Text = ""; } }
-
Open the
Mod
file and replace the contents of the class with the following:Value To Set Edit Box.cs Use dark colors for code blocks Copy class ModValueToSetEditBox : ArcGIS.Desktop.Framework.Contracts.EditBox { public ModValueToSetEditBox() { Module1.Current.ModValueToSetEditBox1 = this; Text = ""; } }
-
Update the
Update
file and add the following line to theValues Button.cs On
method:Click This code allows the button’s
On
method to run theClick Update
method that are added toValues Module1
in a subsequent step.Use dark colors for code blocks Copy Module1.Current.UpdateValues();
Update code in the Config.daml
and Module1.cs
files
-
Open the
Config.daml
file and replace the contents of the groups and controls sections with the following:This code updates:
-
The
Controls
section, adding edit box controls to the UI and setting their attributes; it also updates the button values. -
The
Groups
section, with updated caption and order. The new controls group will be shown on the Add-In tab.
Use dark colors for code blocks Copy <groups> <group id="EditWithInspector_Group1" caption="Update Attributes with Inspector" appearsOnAddInTab="true"> <editBox refID="EditWithInspector_AttributeNameToUseEditBox" size="middle" /> <editBox refID="EditWithInspector_ModValueToSetEditBox" size="middle" /> <button refID="EditWithInspector_UpdateValuesButton" size="large" /> </group> </groups> <controls> <editBox id="EditWithInspector_ModValueToSetEditBox" caption="Enter value to set: " className="ModValueToSetEditBox" sizeString="AAAAAAAAAAAAAAAAA"></editBox> <editBox id="EditWithInspector_AttributeNameToUseEditBox" caption="Enter attribute name to update:" className="AttributeNameToUseEditBox" sizeString="AAAAAAAAAAAAAAAAA"></editBox> <button id="EditWithInspector_UpdateValuesButton" caption="Update Values" className="UpdateValuesButton" loadOnClick="true" smallImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue16.png" largeImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue32.png"> </button> </controls>
-
-
Open the
Module1.cs
file and paste the following below theOverrides
region:It is helpful to consolidate business logic in the Module when possible. This code provides the following:
- References the edit box controls for their use in the module.
- Includes a new method,
Update
, which will be called by the button. This method does the following:Values - Defines a
Queued
in which to call synchronous methodsTask - Checks to make sure that conditions are met for the edit operation to be attempted with the
Inspector
, such as:- Checks for an active
mapview
- Checks for a selected layer in the Contents pane, and that it is for a feature layer with selected records
- Checks that the two edit boxes are populated, and checks that the attribute name is a valid attribute name for the feature layer
- Checks for an active
- Defines a
- Provides a messagebox with a list of the parameters to be used in the editing operation
- Creates an
Inspector
andEdit
Operation - Runs the
Inspector
to calculate the attributes - Applies the
Edit
Operation
Use dark colors for code blocks Copy #region Business Logic // Create instances of the edit boxes for use in the UpdateValues method public AttributeNameToUseEditBox AttributeNameToUseEditBox1 { get; set; } public ModValueToSetEditBox ModValueToSetEditBox1 { get; set; } public void UpdateValues() { // This sample is intended for use with a featureclass with a default text field named "Description". // You can replace "Description" with any field name that works for your dataset // Check for an active mapview if (MapView.Active == null) { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("No MapView currently active. Exiting...", "Info"); return; } QueuedTask.Run(() => { // Get the layer selected in the Contents pane, and prompt if there is none: if (MapView.Active.GetSelectedLayers().Count == 0) { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("No feature layer selected in Contents pane. Exiting...", "Info"); return; } // Check to see if there is a selected feature layer var featLayer = MapView.Active.GetSelectedLayers().First() as FeatureLayer; if (featLayer == null) { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("A feature layer must be selected. Exiting...", "Info"); return; } // Get the selected records, and check/exit if there are none: var featSelectionOIDs = featLayer.GetSelection().GetObjectIDs(); if (featSelectionOIDs.Count == 0) { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("No records selected for layer, " + featLayer.Name + ". Exiting...", "Info"); return; } // Ensure there are values in the two edit boxes else if (ModValueToSetEditBox1.Text == "" || AttributeNameToUseEditBox1.Text == "") { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Editbox for attribute name or value to set is empty. Type in value. Exiting...", "Value Needed"); return; } // Get the name of the attribute to update, and the value to set: string attributename = AttributeNameToUseEditBox1.Text; attributename = attributename.ToUpper(); string setvalue = ModValueToSetEditBox1.Text; // Display all the parameters for the update: ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Here are your update parameters: " + "\r\n Layer: " + featLayer.Name + "\r\n Attribute name: " + attributename + "\r\n Number of records: " + Convert.ToString(featSelectionOIDs.Count) + "\r\n Value to update: " + Convert.ToString(setvalue), "Update"); try { // Now ready to do the actual editing: // 1. Create a new edit operation and a new inspector for working with the attributes // 2. Check to see if a valid field name was chosen for the feature layer // 3. If so, apply the edit // var inspector = new ArcGIS.Desktop.Editing.Attributes.Inspector(); inspector.Load(featLayer, featSelectionOIDs); if (inspector.HasAttributes && inspector.Count(a => a.FieldName.ToUpper() == attributename.ToUpper()) > 0) { inspector[attributename] = setvalue; var editOp = new EditOperation(); editOp.Name = "Edit " + featLayer.Name + ", " + Convert.ToString(featSelectionOIDs.Count) + " records."; editOp.Modify(inspector); editOp.ExecuteAsync(); ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Update operation completed.", "Editing with Inspector"); } else { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("The Attribute provided is not valid. " + "\r\n Ensure your attribute name is correct.", "Invalid attribute"); // return; } } catch (Exception exc) { // Catch any exception found and display a message box. ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught while trying to perform update: " + exc.Message); return; } }); } #endregion
Build and test your code
-
Once your code is complete, build your project and debug any issues. Start ArcGIS Pro, and when the start page appears, select the previously-installed ArcGIS Pro sample project
C
.:\ Data\ Feature Test\ Feature Test.aprx -
When the project has loaded, from the Add-In tab, find your new add-in’s group labeled Update Attributes with Inspector. This confirms that your add-in was properly built and added. Next, select a feature layer from the Contents pane, such as
Test
, and right-click to open its attribute table view. Select a few records and update their Description attribute. Ensure you have the correct layer selected in the TOC, and a few features selected and shown in the attribute table.Points -
Enter values into your add-in’s edit boxes:
-
For attribute name, enter
Description
. -
Since the TestPoints’ Description attribute is a string, enter any value.
-
-
Click Update Values to run the edit operation and update the selected records. The operation should report if a step is missing, and confirm if the edit operation completed successfully.
-
If the operation completes successfully, confirm that your value was applied to the selected records.
What's Next?
Learn how to use additional ArcGIS Pro SDK for .NET features in these tutorials: