<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: LegendUrl not set when loading WMS with sublayers in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/legendurl-not-set-when-loading-wms-with-sublayers/m-p/1614657#M87153</link>
    <description>&lt;P&gt;When setting the `sublayers` property directly in the WMSLayer's constructor, this will override the properties returned from the service's GetCapabilities response, which is why the LegendURL gets stripped.&lt;/P&gt;&lt;P&gt;To maintain all the sublayer's properties from the service, we would recommend setting the sublayers property after the layer has loaded. For example:&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const layer = new WMSLayer({
  url: // url to the service
});
await layer.load();
// filter by a given sublayer
const sublayer = layer.findSublayerByName("My Sublayer");
if (sublayer) {
  layer.sublayers = [sublayer];
}
map.add(layer);&lt;/LI-CODE&gt;&lt;P&gt;You'll see this pattern promoted throughout the SDK documentation, for instance in the &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-WMSLayer.html#properties-summary" target="_self"&gt;API reference&lt;/A&gt; and &lt;A href="https://developers.arcgis.com/javascript/latest/sample-code/layers-wms/" target="_self"&gt;sample&lt;/A&gt;.&lt;/P&gt;</description>
    <pubDate>Wed, 14 May 2025 17:36:03 GMT</pubDate>
    <dc:creator>AnneFitz</dc:creator>
    <dc:date>2025-05-14T17:36:03Z</dc:date>
    <item>
      <title>LegendUrl not set when loading WMS with sublayers</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/legendurl-not-set-when-loading-wms-with-sublayers/m-p/1614440#M87146</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;in my Arc JS App I use the legend widget.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;private _legendWidget?: Legend;

    @ViewChild('legendDiv', { static: false }) private _legendDiv?: ElementRef;

    async ngAfterViewInit(): Promise&amp;lt;void&amp;gt; {
        const view = await this._mvp.getMapViewAsync();
        this._legendWidget = new Legend({ view, container: this._legendDiv?.nativeElement });
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I add a WMS Layer with sublayers I recognize that these are not shown in the legend widget.&lt;BR /&gt;E.G. from the following WMS I try using the sublayer with name "&lt;SPAN&gt;dwd:Ewam_reg025_fd_sl_SHTS&lt;/SPAN&gt;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://maps.dwd.de/geoserver/ows?service=WMS&amp;amp;version=1.3.0&amp;amp;request=GetCapabilities" target="_blank"&gt;https://maps.dwd.de/geoserver/ows?service=WMS&amp;amp;version=1.3.0&amp;amp;request=GetCapabilities&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There I can see&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;Style&amp;gt;
	&amp;lt;Name&amp;gt;ewam_reg025_fd_sl_shts&amp;lt;/Name&amp;gt;
	&amp;lt;Title&amp;gt;Wellenhöhe&amp;lt;/Title&amp;gt;
	&amp;lt;Abstract&amp;gt;style for wind waves and sewll isoflaechen&amp;lt;/Abstract&amp;gt;
	&amp;lt;LegendURL width="91" height="252"&amp;gt;
		&amp;lt;Format&amp;gt;image/png&amp;lt;/Format&amp;gt;
		&amp;lt;OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="https://maps.dwd.de/geoserver/ows?service=WMS&amp;amp;version=1.3.0&amp;amp;request=GetLegendGraphic&amp;amp;format=image%2Fpng&amp;amp;width=20&amp;amp;height=20&amp;amp;layer=dwd%3AEwam_reg025_fd_sl_SHTS"/&amp;gt;
	&amp;lt;/LegendURL&amp;gt;
&amp;lt;/Style&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So it seems all necessary information are in place but somehow the legend url is not taken by arcgis.&lt;BR /&gt;When debugging I can see at the layer object that legendEnabled is set to true while legendUrl is null.&lt;/P&gt;&lt;P&gt;When I manually insert the legend URL into the property of the layer, the legend widget does show the legend correctly.&lt;/P&gt;&lt;P&gt;This is how we load the layer:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;private getWmsLayerProperties(wmsLayerConfig: WmsLayerConfig) {
	const properties: __esri.WMSSublayerProperties = {};
	properties.name = "dwd:Ewam_reg025_fd_sl_SHTS";

	return {
		id: wmsLayerConfig.id,
		url: wmsLayerConfig.url,
		title: wmsLayerConfig.title,
		opacity: wmsLayerConfig.opacity,
		refreshInterval: wmsLayerConfig.refreshInterval,
		sublayers: [properties],
		visible: wmsLayerConfig.visible ?? false
	}
}

private loadLayer() {
	const layer = new WMSLayer(this.getWmsLayerProperties(config));
	layer.load();
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From my understanding the layer.load does some magic including to load the defined sublayers.&lt;BR /&gt;But unfortunately by this the legendUrl is not taken and keeps beeing null.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Of course I could add the legendUrl by hand but we are talking about much layers and those the user may indiviudally add to the map. So I need to find a way to either get ArcGIS do that job for me or else to dynamically load/find the legend url of the given sub layer.&lt;/P&gt;&lt;P&gt;Obviously I would prefer the first. Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 14 May 2025 08:25:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/legendurl-not-set-when-loading-wms-with-sublayers/m-p/1614440#M87146</guid>
      <dc:creator>SebastianKrings</dc:creator>
      <dc:date>2025-05-14T08:25:02Z</dc:date>
    </item>
    <item>
      <title>Re: LegendUrl not set when loading WMS with sublayers</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/legendurl-not-set-when-loading-wms-with-sublayers/m-p/1614477#M87147</link>
      <description>&lt;P&gt;We did a debugging in the dev console of the browser.&lt;/P&gt;&lt;P&gt;In ArcGIS Online we used the default add layer function, to add the layer and the specific sublayer&lt;/P&gt;&lt;P&gt;We found in addfromurl.js line 553 this code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;   else if ("Style" !== Q.nodeName || I.legendUrl)
                            "Layer" === Q.nodeName &amp;amp;&amp;amp; (Q = n(Q, J, M)) &amp;amp;&amp;amp; (Q.parentLayerId = I.id,
                            I.sublayers || (I.sublayers = []),
                            I.sublayers.push(Q));
                        else if (Q = t("LegendURL", Q))
                            if (Q = t("OnlineResource", Q))
                                I.legendUrl = Q.getAttribute("xlink:href")
                    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;which i called within a.parsecapabilities method.&lt;/P&gt;&lt;P&gt;So esri has routines to solve the legendUrl from sublayer by itself. Seems this is not included in the layer.load() function.&lt;/P&gt;</description>
      <pubDate>Wed, 14 May 2025 11:05:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/legendurl-not-set-when-loading-wms-with-sublayers/m-p/1614477#M87147</guid>
      <dc:creator>SebastianKrings</dc:creator>
      <dc:date>2025-05-14T11:05:14Z</dc:date>
    </item>
    <item>
      <title>Re: LegendUrl not set when loading WMS with sublayers</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/legendurl-not-set-when-loading-wms-with-sublayers/m-p/1614488#M87149</link>
      <description>&lt;P&gt;just found another interesting thing&lt;/P&gt;&lt;P&gt;at runtime the layer contains a resourceInfo property which contain all 198 sublayers including their respective legendUrl.&lt;/P&gt;&lt;P&gt;This brought me to the idea of not defining the desired sublayername in the properties at first. With the aim to get all layers with their legendUrl. After this I may filter all unwanted layers out.&lt;BR /&gt;The good news it really happens like that, all sublayers are loaded automatically with their legendUrl.&lt;BR /&gt;The bad new is, I do not know how to properly remove all 197 unwanted layers.&lt;BR /&gt;While layer.allSublayers shows a flattened list layer.sublayers shows a hierarchical list.&lt;/P&gt;&lt;P&gt;Do I only edit the layer.sublayers collection and put my desired sublayer as single/flat item into this collection without any parents (I dont want them)?&lt;BR /&gt;Do I also need to update the layer.allSublayers collection or will it somehow automatically update itself?&lt;BR /&gt;Neither?&lt;BR /&gt;&lt;BR /&gt;Or do I keep my old way defining the sublayer name in the properties and after layer.load() update the sublayer with the legendUrl from the layer.resourceInfo?&lt;BR /&gt;But dont I need to re-run layer.load after updating the legendUrl or will the legendWidget recognize this?&lt;BR /&gt;I would prefer the most clean and safe way to get rid of that.&lt;/P&gt;</description>
      <pubDate>Wed, 14 May 2025 11:44:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/legendurl-not-set-when-loading-wms-with-sublayers/m-p/1614488#M87149</guid>
      <dc:creator>SebastianKrings</dc:creator>
      <dc:date>2025-05-14T11:44:00Z</dc:date>
    </item>
    <item>
      <title>Re: LegendUrl not set when loading WMS with sublayers</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/legendurl-not-set-when-loading-wms-with-sublayers/m-p/1614657#M87153</link>
      <description>&lt;P&gt;When setting the `sublayers` property directly in the WMSLayer's constructor, this will override the properties returned from the service's GetCapabilities response, which is why the LegendURL gets stripped.&lt;/P&gt;&lt;P&gt;To maintain all the sublayer's properties from the service, we would recommend setting the sublayers property after the layer has loaded. For example:&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const layer = new WMSLayer({
  url: // url to the service
});
await layer.load();
// filter by a given sublayer
const sublayer = layer.findSublayerByName("My Sublayer");
if (sublayer) {
  layer.sublayers = [sublayer];
}
map.add(layer);&lt;/LI-CODE&gt;&lt;P&gt;You'll see this pattern promoted throughout the SDK documentation, for instance in the &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-WMSLayer.html#properties-summary" target="_self"&gt;API reference&lt;/A&gt; and &lt;A href="https://developers.arcgis.com/javascript/latest/sample-code/layers-wms/" target="_self"&gt;sample&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 14 May 2025 17:36:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/legendurl-not-set-when-loading-wms-with-sublayers/m-p/1614657#M87153</guid>
      <dc:creator>AnneFitz</dc:creator>
      <dc:date>2025-05-14T17:36:03Z</dc:date>
    </item>
    <item>
      <title>Re: LegendUrl not set when loading WMS with sublayers</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/legendurl-not-set-when-loading-wms-with-sublayers/m-p/1614750#M87155</link>
      <description>&lt;P&gt;The way using&amp;nbsp;findSublayerByName is great, it saves me from doing a recursive search on my own.&lt;/P&gt;&lt;P&gt;May the sample be a bit outdated but layer.sublayers is a collection and therefore not assignable with an array as suggested by you and used in the sample.&lt;/P&gt;&lt;P&gt;My code now is:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;layer.when(() =&amp;gt; {
            if (wantedNames?.length &amp;gt; 0) {
                const wantedSublayers: __esri.WMSSublayer[] = [];
                wantedNames.forEach(name =&amp;gt; {
                    const subLayer = layer.findSublayerByName(name);
                    if (subLayer) {
                        wantedSublayers.push(subLayer);
                    }
                });
                
                layer.sublayers.removeAll();
                layer.sublayers.addMany(wantedSublayers);
            }
            
        });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 May 2025 20:20:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/legendurl-not-set-when-loading-wms-with-sublayers/m-p/1614750#M87155</guid>
      <dc:creator>SebastianKrings</dc:creator>
      <dc:date>2025-05-14T20:20:02Z</dc:date>
    </item>
    <item>
      <title>Re: LegendUrl not set when loading WMS with sublayers</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/legendurl-not-set-when-loading-wms-with-sublayers/m-p/1614763#M87156</link>
      <description>&lt;P&gt;The sublayers property supports &lt;A href="https://developers.arcgis.com/javascript/latest/autocasting/" target="_self"&gt;autocasting&lt;/A&gt;, so it should still work if you set it from an array. As of version 4.32, &lt;A href="https://developers.arcgis.com/javascript/latest/release-notes/#autocasting-enhancements" target="_self"&gt;autocasting is now supported in TypeScript applications&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 14 May 2025 20:58:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/legendurl-not-set-when-loading-wms-with-sublayers/m-p/1614763#M87156</guid>
      <dc:creator>AnneFitz</dc:creator>
      <dc:date>2025-05-14T20:58:55Z</dc:date>
    </item>
  </channel>
</rss>

