How to Create Field in Table, Standalone Table, Feature Layer, or Feature Class?

964
2
06-12-2019 09:42 AM
JamesKennedy1
New Contributor III

Hi,

I am working on porting old applications from ArcObjects to the new ArcGIS Pro SDK and have a significant question. I've looked through the questions and the examples, but I am stumped on this one.

How can I create a field in a Table?

in a Standalone Table?

in a Feature Layer?

in a Feature Class?

In ArcObjects this was quite easy as you could do 

IField field = new Field();
IFieldEdit fieldEdit = (IFieldEdit)field;
fieldEdit.Name_2 = fieldName;
fieldEdit.Type_2 = fieldType;
featureClass.AddField(field);

now I must use ExecuteToolAsync?

How can I use this, is there a good example of someone adding a field?

Thanks,

James

0 Kudos
2 Replies
by Anonymous User
Not applicable

James,

Schema edits in Pro are done with GP tools via the GP.ExecuteToolAsync method as you suspected.

Here's an example of a few calls, with an add field at the end.

namespace testGPExecute
{
  internal class GPExecute : Button
  {
    protected override async void OnClick()
    {
      //create feature class from template
      var mva = Geoprocessing.MakeValueArray(@"C:\arcgis\ArcTutor\Editing\Zion.gdb", "test", "POINT",@"C:\arcgis\ArcTutor\Editing\Zion.gdb\Ranger_stations");
      var cts = new System.Threading.CancellationTokenSource();
      var result = await Geoprocessing.ExecuteToolAsync("CreateFeatureclass_management", mva, null, cts.Token,null,GPExecuteToolFlags.RefreshProjectItems);
      
      //copy selected features from a layer in the map to feature class
      mva = Geoprocessing.MakeValueArray("Ranger stations", @"C:\arcgis\ArcTutor\Editing\Zion.gdb\test");
      result = await Geoprocessing.ExecuteToolAsync("CopyFeatures_management", mva,null,cts.Token,null,GPExecuteToolFlags.None);

      //add field to test feature class
      mva = Geoprocessing.MakeValueArray(@"C:\arcgis\ArcTutor\Editing\Zion.gdb\test", "someID", "LONG");
      result = await Geoprocessing.ExecuteToolAsync("AddField_management", mva, null, cts.Token, null, GPExecuteToolFlags.None);

Note that if the layers are all in your map you can specify the layer name instead of the data path. e.g. "Ranger stations" is a layer in my map so I can use the layer name.

JamesKennedy1
New Contributor III

Thanks Sean,

I'll give this a shot 

0 Kudos