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?
Solved! Go to Solution.
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);
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);