Skip to main content

Portal Items & Web Maps

esri-gl can resolve an ArcGIS portal item id directly into a ready-to-render service, so you don't have to know the underlying service URL. This is powered by @esri/arcgis-rest-portal and supports two shapes: a single-layer item, and a full Web Map.

Both helpers accept the standard authentication options (token, apiKey, or authentication) plus an optional portal (sharing REST URL, defaults to ArcGIS Online).

Resolve a single item

serviceFromPortalItem(sourceId, map, itemId, options?) fetches the item with getItem, maps its type to the matching esri-gl service, constructs it, and adds its source to the map.

import { serviceFromPortalItem } from 'esri-gl';

const { service, kind, url } = await serviceFromPortalItem(
'my-source',
map,
'a1b2c3d4e5f6…', // portal item id
{ apiKey: 'AAPK…' }
);

map.addLayer({ id: 'my-layer', type: 'raster', source: 'my-source' });

The promise resolves to a PortalServiceResult:

FieldTypeDescription
serviceservice instanceThe constructed esri-gl service.
kind'dynamic' | 'tiled' | 'image' | 'vector-tile' | 'feature'Which service was created.
sourceIdstringThe source id registered on the map.
urlstringThe service URL the item resolved to.
itemIItemThe portal item metadata.
titlestringThe item title.

Item type → service mapping

Portal item typeesri-gl service
Feature Service / Feature LayerFeatureService
Map ServiceDynamicMapService (or TiledMapService when the item is cached/tiled)
Image ServiceImageService
Vector Tile ServiceVectorTileService

For a multi-layer Feature Service whose item URL points at the service root, esri-gl targets sublayer 0 by default — pass layerId to choose another:

await serviceFromPortalItem('my-source', map, itemId, { apiKey, layerId: 2 });

Options

PortalItemServiceOptions extends the auth options with:

OptionTypeDescription
portalstringPortal sharing REST URL (defaults to ArcGIS Online).
layerIdnumberFeature Service sublayer to load (default 0).
serviceOptionsobjectExtra options merged into the constructed service.
rasterSrcOptionsobjectSource options for Dynamic / Tiled / Image services.
vectorSrcOptionsobjectSource options for the Vector Tile service.
geojsonSourceOptionsobjectSource options for the Feature service.

Resolve a Web Map

servicesFromWebMap(map, itemId, options?) reads a Web Map's data with getItemData and instantiates a service for each supported operational layer (optionally including the basemap). Unsupported layer types are skipped.

import { servicesFromWebMap } from 'esri-gl';

const layers = await servicesFromWebMap(map, 'webmap-item-id', {
token: 'eyJ…',
includeBasemap: false,
});

layers.forEach(({ service, kind, sourceId, title }) => {
// add each service's source to the map as appropriate for its `kind`
console.log(`${title}${kind} (${sourceId})`);
});

Each entry is a PortalServiceResult (without item). Source ids are generated as ${prefix}-${layerId} (the prefix defaults to the Web Map item id; override with sourceIdPrefix).

Web Map layer type → service mapping

Operational layer layerTypeesri-gl service
ArcGISFeatureLayerFeatureService
ArcGISMapServiceLayerDynamicMapService
ArcGISTiledMapServiceLayerTiledMapService
ArcGISImageServiceLayerImageService
VectorTileLayerVectorTileService

Options

WebMapOptions extends PortalItemServiceOptions with:

OptionTypeDescription
includeBasemapbooleanAlso instantiate the Web Map's basemap layers (default false).
sourceIdPrefixstringPrefix for generated source ids (default the Web Map item id).