How to get the layer url when Adding a layer

986
2
08-16-2018 02:12 AM
ChaoWang
Esri Contributor

Recently, I used the SDK to make a small plug-in. I need to get the URL of the layer when adding the layer. In fact, only when the type of the layer is ServiceLayer can I get the URL. But if I want to add a local data, similar to "C:\temp\test.shp", but in the Layer property can not get the URL (or Path, DataSource and other properties), so, is the SDK designed like this or still can use other method to get the URL.

Attach some code (Sure, is URL property, not URI property )

 LayersAddedEvent.Subscribe(AddLayer, false);

private void AddLayer(LayerEventsArgs args)
{

   List<Layer> layerList = args.Layers as List<Layer>;
   if (layerList != null && layerList.Count != 0)

   Layer layer = layerList[0] as Layer;
   string url = GetLayerUrl(layer);

}

private string GetLayerUrl(Layer layer)
{
// Determine whether the layer has URL property, if has, get URL value.
return layer?.GetType()?.GetProperty("URL")?.GetValue(layer) as string;
}

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

Hi Chao,

 I am not sure if you are looking for the actual 'datasource' of the Layer.  If so:

Layer inherits MapMember and you can use MapMember properties and methods to gain access to CIMDataConnection  Once you have the CIMDataConnection you have to check what type of specific connection you are dealing with (i.e. CIMStandardDataConnection ) as shown this this Button 'OnClick' sample snippet:

protected override void OnClick()
{
  // Arbitrarily, pick the first feature layer - just assuming there is one!
  var layer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().ToList()[0];
  QueuedTask.Run(() =>
  {
    CIMDataConnection currentDataConnection = layer.GetDataConnection();
    // check the specific type:
    if (currentDataConnection is CIMStandardDataConnection)
    {
      var standardDC = currentDataConnection as CIMStandardDataConnection;
    MessageBox.Show($@"WorkspaceConnectionString: {standardDC.WorkspaceConnectionString}
Dataset: {standardDC.Dataset}
DatasetType: {standardDC.DatasetType}");
    }
        
  });
}

When I run this on my map (which geodatabase layers on the top), I get this output:

with this Map:

ChaoWang
Esri Contributor

Thanks , the issue has been solved.

0 Kudos