How to set a layer name while adding a new layer

1119
7
09-28-2020 12:43 PM
MichelTurmel
New Contributor III

I am in the process of creating my first custom 2 D widget and I can dynamically add a layer on the map but when I use the standard Layer List widget I see that the name of the layer I added to the map is  the name of the Map Service that I have used. Is there a way to set the name of the new layer that will be displayed when I use the standard Layer List widget?

Here is my environment:

   - Portal for ArcGIS version 10.6

   - Web AppBuilder Developer Edition 2.6

   - ArcGIS JavaScript API 3.33

0 Kudos
7 Replies
RobertScheitlin__GISP
MVP Emeritus

Michel,

   I set several layer properties in my widgets.

layer._titleForLegend = layer.title = layer.name = 'blah';

0 Kudos
MichelTurmel
New Contributor III

Good morning Robert,

Thank you for the reply, is there a sample widget url that you could send me?

Am I right to assume that I will be able to use “layer._titleForLegend = layer.title = layer.name<https://urldefense.proofpoint.com/v2/url?u=http-3A__layer.name&d=DwQCaQ&c=H50I6Bh8SW87d_bXfZP_8g&r=k3rHcdYwBizPCV7b7qgyUp_gjpgw6vSQ0uK1EqfctxM&m=4sZH_igkBdNWXpHFicvptdY2VRbC-kg3mtYXdyRTM3A&s=snZ27qhuj0u1lZj_txlk_UasN0p53vCRwThw9h-A5nQ&e=> = 'blah';” only as part of event “layers-add-result” after the map.addLayers(); is completed?

Michel Turmel

Senior Consultant

CGI

Email: michel.turmel@cgi.com<mailto:michel.turmel@cgi.com>

Phone: (306) 761-4000 Ext: 1354384

1900 Albert Street

Regina, Saskatchewan

S4P 4K8

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Michel,

  No you do not wait for the layer to be added before you apply those properties. After you use the layer constructor you apply those properties before the layer is added to the map.

var myfl = new FeatureLayer("your url", {...});
myfl_titleForLegend = myfl.title = myfl.name = 'blah';
this.map.addLayer(myfl);

You can find this property ("_titleForLegend") in the otb AddData widget and in my eSearch Widget.

https://community.esri.com/docs/DOC-1731-enhanced-search-widget-version-213-092519 

0 Kudos
MichelTurmel
New Contributor III

Thank you very much Robert,

This works great!

Michel Turmel

Senior Consultant

CGI

Email: michel.turmel@cgi.com<mailto:michel.turmel@cgi.com>

Phone: (306) 761-4000 Ext: 1354384

1900 Albert Street

Regina, Saskatchewan

S4P 4K8

0 Kudos
JohnLucotch2
Occasional Contributor

Hey Robert,

I'm running into this issue as well but I want just the default title to be the layer name of the Feature Layer instead of the Map Service.  Any ideas? I want to use the name in the Layer 1 Name: Layer: Incorporated Place labels (ID: 1) 

0 Kudos
MichelTurmel
New Contributor III

Hello John,

You will need to set the layer name in a variable and then apply it to the layer you are about to add:

var template = new esri.InfoTemplate();

var newLayerName = "new layer name"

var newLayerSearch = "....."

var newLayerURL = "https://..........";

var newLayerFeatureLayer = new esri.layers.FeatureLayer(newLayerURL, {

   id: newLayerName,

   definitionExpression: newLayerSearch

});

// the following line will provide the layer name displayed on the Layer List:

newLayerFeatureLayer._titleForLegend=newLayerFeatureLayer.title=newLayerFeatureLayer.name=newLayerName

map.addLayers([newLayerFeatureLayer]);

Hope this helps!

Michel Turmel

0 Kudos
JohnLucotch2
Occasional Contributor

Thanks but not what I was hoping for.  I don’t want to use the name variable.  I want to use the default layer name similar to the add data widget. 

0 Kudos