Call AddFields_management from c#

2291
5
Jump to solution
11-13-2017 05:53 AM
RehmSoftware
New Contributor III

Hey,

I'm trying to call AddFields_management via the Geoprocessing.ExecuteToolAsync function. And while I get other python tools working flawlessly I have a hard time adding multiple fields via AddFields_management. My main issue is how to pass the field description parameter which is a nested list in python. I tried jagged arrays, nested List<> but nothing worked.

So given the "official" example..

import arcpy

arcpy.env.workspace = "C:/data/district.gdb"

arcpy.management.AddFields('school', [['school_name', 'TEXT', 'Name', 255, 'Hello world'], ['street_number', 'LONG', 'Street Number', None, 35]])

How would that look in C#?

Thanks in advance & Best regards

Christian

Tags (2)
1 Solution

Accepted Solutions
NarelleChedzey
Esri Contributor

Hi Christian, 

Generally if I have a problem working out the structure of GP parameters I find it useful to run the tool within ArcGIS Pro and then find the geoprocessing history, hover over the tool I just ran and see what the popup shows me in terms of parameters.

This is what I got when I ran the Add Fields routine  (adding a field1 of type long with alias 'field1'  and field2 of type text with field length of 255 and default value of 'MyDefaultValue'

this would then translate into .NET as the following

await QueuedTask.Run(async () =>
{
    var testLines = @"C:\data\SampleGDB.gdb\test_lines";

    // set up the arguments
    var fields = "field1 long field1 # #;field2 Text # 255 myDefaultValue";

    var args = Geoprocessing.MakeValueArray(testLines, fields);

    // run the tool

    var result = await Geoprocessing.ExecuteToolAsync("management.AddFields", args);
});

Hope this helps

Narelle

View solution in original post

5 Replies
NarelleChedzey
Esri Contributor

Hi Christian, 

Generally if I have a problem working out the structure of GP parameters I find it useful to run the tool within ArcGIS Pro and then find the geoprocessing history, hover over the tool I just ran and see what the popup shows me in terms of parameters.

This is what I got when I ran the Add Fields routine  (adding a field1 of type long with alias 'field1'  and field2 of type text with field length of 255 and default value of 'MyDefaultValue'

this would then translate into .NET as the following

await QueuedTask.Run(async () =>
{
    var testLines = @"C:\data\SampleGDB.gdb\test_lines";

    // set up the arguments
    var fields = "field1 long field1 # #;field2 Text # 255 myDefaultValue";

    var args = Geoprocessing.MakeValueArray(testLines, fields);

    // run the tool

    var result = await Geoprocessing.ExecuteToolAsync("management.AddFields", args);
});

Hope this helps

Narelle

RehmSoftware
New Contributor III

Hi Narelle,

thank you very much. I'm especially thankful for "help to help myself" such as the info you gave me!

Best regards

Christian

0 Kudos
deleted-user-JTyWy9b20ETO
New Contributor II

This worked for me, but I noticed that while AddField() happily accepts a field of type STRING, AddFields() will not and you must request a field of type TEXT or one of the other  allowed types.

0 Kudos
GwenRoyakkers
New Contributor III

Hi,

I solved this problem by creating the following function:

private static async Task AddField(string table, string fieldName, string fieldType, string precision = "", 
    string scale = "", string length = "", string alias = "")
{
    List<object> arguments = new List<object>();
    arguments.Add(table);
    arguments.Add(fieldName);
    arguments.Add(fieldType);
    arguments.Add(precision);
    arguments.Add(scale);
    arguments.Add(length);
    arguments.Add(alias);

    IGPResult result = await Geoprocessing.ExecuteToolAsync(
        "management.AddField", Geoprocessing.MakeValueArray(arguments.ToArray())
    );
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Then i just call it like

await AddField(exportFeatureClass, "TextFieldName", "TEXT", length: "16");
// exportfeatureclass is the full path to the table or featureclass like c:\test.gdb\testFC‍‍

You don't have to use the QueuedTask.Run() because in this case none of the used method's require the MCT to run.

If you want to add multiple fields you'll have to que them (internally this happens with AddFields_management).

You could do this in a loop, or by calling the function for every field you want to add.

Best regards,

Gwen.

0 Kudos
RehmSoftware
New Contributor III

Hi Gwen,

thanks for your reply. I'm aware that there's AddField (singular) and I got that working already. I just throught it's better to call AddFields when adding a bunch of fields instead of looping over AddField. Thanks anyway!

Best regards

Christian