Remove Layer from ListView Programticaly

1616
7
02-18-2020 11:07 AM
OracleEsri
New Contributor

Hello everyone, I need to used API to return permissions on layers If the user haven't permission hide/ remove a layer from the map.

0 Kudos
7 Replies
LeoLiu1
Occasional Contributor

How about this?

Map | API Reference | ArcGIS API for JavaScript 3.31 

Then in your widget, use

this.Map.removeLayer(YOURLAYEROBJECT);
OracleEsri
New Contributor

Thanks for your replaying, but when calling API using (Esri/request) needs to be asynchronous.

0 Kudos
LeoLiu1
Occasional Contributor

Ahh, OK. In that case, it's probably not a good idea to control the layers' permission within a custom widget as the widget is quite often created later than the map.

It's better setting up user permissions on the source layers of your web map.

So the response of the map request doesn't contain the in-accessible layers at all.

Cheers,

Leo.

OracleEsri
New Contributor

Thanks, Leo, Can you give me a sample of codes as examples. 

0 Kudos
HushamMohamed
Occasional Contributor

I really found Robert Scheitlin, GISP  answer here  Very helpful.

hope it help you as Well.

OracleEsri
New Contributor

Thanks for your response,  but when calling API using (Esri/request)  in (ListLayer) needs to be asynchronous. where the layer appears before the response from the remote server received.

0 Kudos
by Anonymous User
Not applicable

Hi Oracle Esri‌,

Here is the sample code for you.

I assume you create layer with something like var layer = new FeatureLayer(... kind of style.

Below is the sample code, you might see that console.warn layer access error statement. I can manage to catch user accessibly to that layer at that place.

_waitForLayer: function (layer) {
        var dfd = new Deferred(),
          handles = [];
        if (layer.loaded) {
          dfd.resolve(layer);
          return dfd;
        }
        if (layer.loadError) {
          dfd.reject(layer.loadError);
          return dfd;
        }
        var clearHandles = function () {
          array.forEach(handles, function (h) {
            h.remove();
          });
        };
        //console.warn("_waitForLayer");
        handles.push(layer.on("load", function (layerLoaded) {
          //console.warn("_waitForLayer.load",layerLoaded);
          clearHandles();
          dfd.resolve(layerLoaded.layer);
        }));
        handles.push(layer.on("error", function (layerError) {
          //console.warn("_waitForLayer.error",layerError);
          clearHandles();
          var error = layerError.error;
          try {
            if (error.message && (error.message.indexOf("Unable to complete") !== -1)) {
              console.warn("layerAccessError", error);
              dfd.reject(new Error(i18n.search.layerInaccessible));
            } else {
              dfd.reject(error);
            }
          } catch (ex) {
            //console.warn("layerAccessError",ex);
            dfd.reject(error);
          }
        }));
        return dfd;
      },


this._waitForLayer(layer).then(lang.hitch(this, function (layer) {
//layer result is in layer variable
// layer is loaded here
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos