It may work for you. CreateLayerList() method makes a list of layers of current map. It includes layers under group layers also.
IWorkspace GetWorkspace()
{
var mapLayers = GetAllLayersFromMap();
if(mapLayers.Count > 0)
IWorkspace workspace =((IDataset)mapLayers[0]).Workspace;
}
List<ILayer> GetAllLayersFromMap()
{
var mapLayers = new List<ILayer>();
CreateLayerList(null, _map, _map.LayerCount, mapLayers);
return mapLayers;
}
void CreateLayerList(ICompositeLayer compositeLayer, IMap map, int mapCount, List<ILayer> mapLayers)
{
for (int i = 0; i < mapCount; i++)
{
ILayer layer;
if (map != null && compositeLayer == null)
layer = map.Layer[i];
else if (compositeLayer != null)
layer = compositeLayer.Layer[i];
else
return;
ICompositeLayer comLayer;
if (((comLayer = layer as ICompositeLayer) != null) && ((layer as IGroupLayer) != null))
CreateLayerList(comLayer, null, comLayer.Count, mapLayers);
else
mapLayers.Add(layer);
}
}