How to create a field with SDK .NET ?

3663
9
Jump to solution
10-12-2015 06:42 AM
Highlighted
New Contributor II

Hello,

I wish to create a new field (column) in the attribute table of a specific layer with the SDK .Net.

It is possible to create a new row (ligne) in the attribute table with createRow(RowBuffer rowbuffer) but a function to create a field don't seem to existe.

I found in the api reference that there was a field class and an attribute class that allow to get information about a field but nothing to actually create one. All the create function that I found allow to create a feature, or a row but not a field.

I also searched in the sample available on github (Esri/arcgis-pro-sdk-community-samples · GitHub ).

Maybe I missed something in the api reference.

Thank you for your help.

Tags (1)
1 Solution

Accepted Solutions
Highlighted
Esri Contributor

or as it was pointed out to me:

FeatureLayer firstLayer = MapView.Active.Map.Layers.OfType<FeatureLayer>().FirstOrDefault();

await QueuedTask.Run(() =>

{

   FeatureClass fc = firstLayer.GetTable() as FeatureClass;

   Geoprocessing.ExecuteToolAsync("AddField_management", Geoprocessing.MakeValueArray(fc, "FieldName", "LONG", 9));

});

View solution in original post

9 Replies
Highlighted
MVP Frequent Contributor

I don't know but if it is missing you can use 'add field' of gp (https://github.com/Esri/arcgis-pro-sdk-community-samples/tree/master/Geoprocessing)

Reply
0 Kudos
Highlighted
New Contributor II

When you say gp, are you talking about DriveTimeGP ?

Because there aren't any add field in this project.

Reply
0 Kudos
Highlighted
Esri Contributor

You are not missing anything in the API. The API is designed following the data manipulation language (DML).

In order to add a field you would make a call using geoprocessing such as

using ArcGIS.Desktop.Core.Geoprocessing;

await QueuedTask.Run(() =>
    {
        Geoprocessing.ExecuteToolAsync("AddField_management", Geoprocessing.MakeValueArray("FieldName", "LONG", 9));
    });

Highlighted
New Contributor II

Thank you but how do you tell this function which attribute table will add the new field ?

And this function use geoprocessing but is it possible to create a field in another way, like the creation of row with createRow() ?

Reply
0 Kudos
Highlighted
Esri Contributor

My mistake,

the first argument is the target table.

using ArcGIS.Desktop.Core.Geoprocessing; 

await QueuedTask.Run(() => 

    { 

        Geoprocessing.ExecuteToolAsync("AddField_management", Geoprocessing.MakeValueArray(@"c:\temp\demo.gdb\sample", "FieldName", "LONG", 9)); 

    });

When you are working with layers in your map this would be that way to get the location:

FeatureLayer firstLayer = MapView.Active.Map.Layers.OfType<FeatureLayer>().FirstOrDefault();

await QueuedTask.Run(() =>
{
FeatureClass fc = firstLayer.GetTable() as FeatureClass;

Datastore ds = fc.GetDatastore();
string dc = ds.GetConnectionString();

if (ds is Geodatabase)
{
  Geodatabase gdb = fc.GetDatastore() as Geodatabase;
  string gdbPath = gdb.GetPath();
}

string name = fc.GetName();
});

Highlighted
Esri Contributor

or as it was pointed out to me:

FeatureLayer firstLayer = MapView.Active.Map.Layers.OfType<FeatureLayer>().FirstOrDefault();

await QueuedTask.Run(() =>

{

   FeatureClass fc = firstLayer.GetTable() as FeatureClass;

   Geoprocessing.ExecuteToolAsync("AddField_management", Geoprocessing.MakeValueArray(fc, "FieldName", "LONG", 9));

});

View solution in original post

Highlighted
New Contributor II

Thank you very much for your help.

One last thing, where does the string "AddField_management" come from ?

I didn't find anything about it on the api reference or in the DAML.cs that you can generate in code.

Reply
0 Kudos
Highlighted
MVP Frequent Contributor

you can use toolboxalias.toolname or toolname_toolboxalias pattern.

The data management toolbox has alias 'management' while the name of tool is 'AddField' (click right button of mouse on tool and select property and see the name)

Here in

https://desktop.arcgis.com/en/desktop/latest/guide-books/extensions/task-assistant-manager/task-assi... you can see the list of alias of toolbox system

Highlighted
New Contributor II

Thank you

Reply
0 Kudos