I need to find a piece of ArcObjects code to Iterate/Loop through all layers of a map service. Appreciate if you can provide the source.
Hello Shaning,
I know how to do this in Python but not ArcObjects. Must your solution be ArcObjects (presuming C#, Java, or VB)?
Micah
My project is an Add-In project. Therefore, I have to use C#. Thanks.
Hi Shaning,
Ok, no problem. I think the same concepts will apply. You'll need to make an http request on the service with JSON as the response format. From there you can retrieve the layer list.
Consider this service:
https://services.nationalmap.gov/arcgis/rest/services/transportation/MapServer
The following HTTP request will get you a JSON representation of that service:
https://services.nationalmap.gov/arcgis/rest/services/transportation/MapServer?f=pjson
Notice that within the response there is a key called "layers" - the corresponding value is a representation of all the layers:
To request more layer-specific info, just modify the request to include the layer ID and you can go deeper into the properties of each layer.
I googled "Make HTTP request C#" and here is the top result:
c# - HTTP request with post - Stack Overflow
Hope this helps.
Micah
Hi Shaning,
If you have access to an IServerObjectHelper object, you can then get an IMapLayerInfos object, which is a collection of IMapLayerInfo.
The only way I've done this is when using SOI and SOE's and in that case you get an IServerObjectHelper instantiated when the SOI/SOE starts up.
/// <summary>
/// IServerObjectExtension implementation
/// This method is called when then service with this SOI is started
/// </summary>
/// <param name="pSOH"></param>
public void Init(IServerObjectHelper pSOH)
{
IMapLayerInfos layerInfos = pSOH.MapServer.GetServerInfo(pSOH.MapServer.DefaultMapName).MapLayerInfos;
// Now you can iterate through layerInfos
IMapLayerInfo layerInfo;
for (int i = 0; i < layerInfos.Count; i++)
{
layerInfo = layerInfos.get_Element(i);
}
}
Not sure how to get access to the IServerObjectHelper object if you're not writing an SOI or SOE. Perhaps someone else can fill in that part!
Cheers,
Jordan