Select to view content in your preferred language

Need help to retrieve the .SDE file path from the FeatureLayer using ArcGIS Pro.NET SDK..

2628
5
06-16-2021 08:30 AM
LakshmiAlaparthi
Emerging Contributor

Hi 

I wrote this function bellow and it is getting me the path like the bellow from the temp folder and I am not able to connect to  the enterprise geodatabase using this path --

C:/Users/<username>/AppData/Local/Temp/ArcGISProTemp19324/a8b12d344fb5de85c96517857bcf6e6e.sde

 Public Function GetConnectionPathoftheSDE(ByVal thelayer As String) As Task(Of String)
Return QueuedTask.Run(Function()
Dim featureLayer = TryCast(MapView.Active.Map.FindLayers(thelayer).First(), BasicFeatureLayer)
Dim inTable = featureLayer.Name
Dim table = featureLayer.GetTable()
Dim dataStore = table.GetDatastore()
Dim connectionString = dataStore.GetPath.ToString()
Dim layerAlias = featureLayer.Name
Return connectionString
End Function)
End Function

 I need to retrieve the catalog path like this -- C:\Users\username\Documents\ArcGIS\Projects\testmap\server111.sde -- this path shows up when I hover the mouse on the layer under the catalog pane -> .SDE connection node. How to retrieve this path using Pro .NET SDK?

Thanks

Lakshmi

0 Kudos
5 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

In ArcGIS Pro the .sde path is not persisted anywhere after you create a layer by adding a feature class to a map. Instead all relevant connection properties are extracted from the .SDE file, if needed the user is prompted for the password, and the individual connection properties are then kept to reconnect the layer. You can utilize a layer’s connection information in code as shown in the sample snippet below. The snippet is taken from a button's OnClick method and uses the database connection from an existing SDE layer to retrieve all feature classes from the layer’s geodatabase.

Wolf_0-1623885423665.png

 

protected override async void OnClick()
{
    var selectedLayer = MapView.Active.GetSelectedLayers().FirstOrDefault();
    // set the sketch layer property (in order to obtain Zs, Ms as appropriate) 
    if (!(selectedLayer is BasicFeatureLayer theLayer))
    {
        MessageBox.Show("Select a feature layer on the TOC first");
        return;
    }
    var lst = await QueuedTask.Run<List<string>>(() =>
    {
        var lstFcs = new List<string>();
        var fc = theLayer.GetTable() as FeatureClass;
        // use the layer's Datastore to access the Geodatabase
        using (var gdb = fc.GetDatastore() as Geodatabase)
        {
            var fcDefs = gdb.GetDefinitions<FeatureClassDefinition>();
            foreach (var tableDef in fcDefs)
            {
                lstFcs.Add(tableDef.GetName());
            }
        }
        return lstFcs;
    });
    MessageBox.Show(string.Join(Environment.NewLine, lst.ToArray()));
}

  

0 Kudos
LakshmiAlaparthi
Emerging Contributor

Got it.. Is there any way to find out from the API? 

If (Not layer.FeatureClass Is Nothing) Then
.Alias = layer.FeatureClass.AliasName
.ConnectionPath = CType(layer.FeatureClass, IDataset).Workspace.PathName

End if 

0 Kudos
LakshmiAlaparthi
Emerging Contributor

This is the code that I was using in ArcObjects .NET 

If (Not layer.FeatureClass Is Nothing) Then
.Alias = layer.FeatureClass.AliasName
.ConnectionPath = CType(layer.FeatureClass, IDataset).Workspace.PathName

 

0 Kudos
LakshmiAlaparthi
Emerging Contributor

Hi, 

Thank you for the response. I am looking for the solution  to retrieve the path of the .sde file that the feature layer belongs to. Once I have the sde file path my code is working fine to open the geodatabase. I am looking for the solution on how to retrieve the .sde file path from the ArcGIS Pro UI -> layer that is loaded in the Table of Contents.

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

It's not possible to retrieve the .sde file path from a layer, since only the connection properties are used by the layer not the original .sde file.  You can verify this by deleting or renaming the .sde file and you will still be able to view your sde data on the map.  But you can use this code to open the Geodatabase instead:

// theLayer is the Mapmember from the TOC
var fc = theLayer.GetTable() as FeatureClass;
// use the layer's Datastore to access the Geodatabase
using (var gdb = fc.GetDatastore() as Geodatabase)
{
   // use the open geodatabase "gdb" here (same a theLayer's datasource)
}

 

0 Kudos