Tip: Load layers using Portal Item ID versus REST endpoint URI

1366
0
04-08-2020 01:25 PM
JimBarry
Esri Regular Contributor
2 0 1,366

When using the ArcGIS API for JavaScript (v4), and creating ~Layer objects, there are two ways to connect to the resource, either by loading the portal item by its portal ID, or by calling the REST endpoint URI directly:

var fl = new FeatureLayer({
  portalItem: {
    id: 'a624a764295e4887b096ab7972b7bd8f'
  }
 });

// OR...

var fl = new FeatureLayer({
 url: "https://services6.arcgis.com/0p6i4J6xhQas4Unf/arcgis/rest/services/SI_TRACKS/FeatureServer"
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

In either case, any rendering, popups, or other settings and configuration that you've saved back with the layer will come through.

If a layer or other resource in the app/page has some sharing perms set on it, making it only accessible by the Owner, or an Organization or Group within, it's by design that the API will open up a modal login dialog to see if the user is allowed to access that resource.

One advantage to loading the layer using its portal ID is when it comes to figuring out which layers are opening a modal dialog when you're not expecting them to. If your map contains many layers, this can be a bit of troubleshooting.

If you created the layer using the url property, the modal login dialog will look like this:

Not very useful.

But, if you created the layer using the portalItem property, the modal login dialog will look like this:

Very useful.

The dialog tells you right there exactly which Portal Item it is trying to open. You can go back into your code or your Content tab and find the layer that may not have its sharing permissions set as you expected. Or perhaps it's an item owned by some other person or organization. Or maybe the content used to exist, and doesn't any longer, and maybe then you can find the source data and rehost it yourself, then change the path/ID. Or just drop that item altogether if maybe it's no longer needed for your map or app.

There may be other pros and cons for creating layers with url or portalItem, but this here is a case where loading by portalItem gives you useful info for troubleshooting.

About the Author
https://x.com/jimbarry https://github.com/jimbarry