Checking if a dataset name exists or not

101
1
Jump to solution
a month ago
MarioLandry2
New Contributor

I'd like to know if there is a simple function that will tell me if a dataset with a specific name exists or not.

Under ArcObjects we used to do this:

pWsSource2 = CType(pOutworkspace, IWorkspace2)
If pWsSource2.NameExists(esriDatasetType.esriDTFeatureClass, "whatever_the_name") Then

...

...

But with ArcGIS Pro SDK, I can only see a solution that does involve a loop like this:

var datasetDefinitions = geodatabaseMTNC.GetDefinitions<ArcGIS.Core.Data.FeatureDatasetDefinition>();

// Loop through the results
foreach (var definition in datasetDefinitions)
{
    if(definition.GetName() == "whatever_the_name")
    {
    // Do something
    }
}

 

Is there another approach?

0 Kudos
1 Solution

Accepted Solutions
ChrisSeabrooke1
New Contributor III

I call the geodatabase.GetDefinition () method.  This method must be called from a QueuedTask.Run.  GetDefinition throws an exception if the definition doesn't exist. If there is another way to determine if a FeatureClass or Table exist, I hope someone will share it.

Here is the code that can be called from a QueuedTask:

public static bool FeatureClassExists(Geodatabase geodatabase, string featureClassName)
{
try
{
FeatureClassDefinition featureClassDefinition = geodatabase.GetDefinition<FeatureClassDefinition>(featureClassName);
featureClassDefinition.Dispose();  // FeatureClass found, dispose of object
return true;
}
catch
{
// GetDefinition throws an exception if the definition doesn't exist
return false;
}
}

If you're checking for a Table, replace FeatureClassDefinition with TableDefinition:

TableDefinition tableDefinition = geodatabase.GetDefinition<TableDefinition>(tableName);

View solution in original post

1 Reply
ChrisSeabrooke1
New Contributor III

I call the geodatabase.GetDefinition () method.  This method must be called from a QueuedTask.Run.  GetDefinition throws an exception if the definition doesn't exist. If there is another way to determine if a FeatureClass or Table exist, I hope someone will share it.

Here is the code that can be called from a QueuedTask:

public static bool FeatureClassExists(Geodatabase geodatabase, string featureClassName)
{
try
{
FeatureClassDefinition featureClassDefinition = geodatabase.GetDefinition<FeatureClassDefinition>(featureClassName);
featureClassDefinition.Dispose();  // FeatureClass found, dispose of object
return true;
}
catch
{
// GetDefinition throws an exception if the definition doesn't exist
return false;
}
}

If you're checking for a Table, replace FeatureClassDefinition with TableDefinition:

TableDefinition tableDefinition = geodatabase.GetDefinition<TableDefinition>(tableName);