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.
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.
<SPAN class="comment token">/// <summary></SPAN> <SPAN class="comment token">/// IServerObjectExtension implementation</SPAN> <SPAN class="comment token">/// This method is called when then service with this SOI is started</SPAN> <SPAN class="comment token">/// </summary></SPAN> <SPAN class="comment token">/// <param name="pSOH"></param></SPAN> <SPAN class="keyword token">public</SPAN> <SPAN class="keyword token">void</SPAN> <SPAN class="token function">Init</SPAN><SPAN class="punctuation token">(</SPAN>IServerObjectHelper pSOH<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> IMapLayerInfos layerInfos <SPAN class="operator token">=</SPAN> pSOH<SPAN class="punctuation token">.</SPAN>MapServer<SPAN class="punctuation token">.</SPAN><SPAN class="token function">GetServerInfo</SPAN><SPAN class="punctuation token">(</SPAN>pSOH<SPAN class="punctuation token">.</SPAN>MapServer<SPAN class="punctuation token">.</SPAN>DefaultMapName<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>MapLayerInfos<SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// Now you can iterate through layerInfos</SPAN> IMapLayerInfo layerInfo<SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">for</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">int</SPAN> i <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">;</SPAN> i <SPAN class="operator token"><</SPAN> layerInfos<SPAN class="punctuation token">.</SPAN>Count<SPAN class="punctuation token">;</SPAN> i<SPAN class="operator token">++</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> layerInfo <SPAN class="operator token">=</SPAN> layerInfos<SPAN class="punctuation token">.</SPAN><SPAN class="token function">get_Element</SPAN><SPAN class="punctuation token">(</SPAN>i<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.