This sample demonstrates how to load a PortalItem from a Portal for ArcGIS item into your application.
1. Define a template to display items
Define a template using a Calcite Card for each PortalItem. The card displays the item’s thumbnail, title, published date, and owner. Setting draggable="true" on the card enables native drag-and-drop. The thumbnail has draggable="false" so dragging the image transfers the card’s item ID rather than the image URL.
const template = '<calcite-card data-itemid="{id}" draggable="true" label="{title}">' + '<img slot="thumbnail" src="{thumbnailUrl}" alt="" draggable="false">' + '<span slot="heading">{title}</span>' + "<dl>" + "<dt>Published date</dt>" + "<dd>{created}</dd>" + "<dt>Owner</dt>" + "<dd>{owner}</dd>" + "</dl>" + "</calcite-card>";2. Load the Portal Items
Create an array of all the Portal items to use in this application. We want to load the Portal items when they are created, because we want to use the information such as id, owner, etc. to display in the application.
const portalItems = layerItems.map((itemid) => { return new PortalItem({ id: itemid, }).load();});3. Create DOM elements to display item details on page
Use the eachAlways method of esri/core/promiseUtils to wait for the Portal items to finish loading. You can then use the esri/intl.substitute() method to create DOM elements using the details of each Portal item to display on the page. Also, you will need to listen for DOM element’s dragStart event and assign some data to be transferred to wherever that element is dropped.
promiseUtils.eachAlways(portalItems).then((items) => { const docFrag = document.createDocumentFragment(); items.map((result) => { const item = result.value; const card = intl.substitute(template, item); const elem = document.createElement("div"); elem.innerHTML = card; // This is a technique to turn a DOM string to a DOM element. const target = elem.firstChild; docFrag.appendChild(target); target.addEventListener("dragstart", (event) => { const id = event.currentTarget.getAttribute("data-itemid"); event.dataTransfer.setData("text", id); }); }); document.querySelector(".cards-list").appendChild(docFrag); ...});4. Manage drag events
Listen for the drop and dragover events on the Map component. You can then get the id of the PortalItem and add the layer to the component’s WebMap using Layer.fromPortalItem.
promiseUtils.eachAlways(portalItems).then((items) => { ... viewElement.addEventListener("dragover", (event) => { event.preventDefault(); event.dataTransfer.dropEffect = "copy"; }) viewElement.addEventListener("drop", (event) => { event.preventDefault(); const id = event.dataTransfer.getData("text"); // Get the first item that matches const resultItem = items.find((x) => { return x.value.id === id; }); const item = resultItem.value; if (item && item.isLayer) { Layer.fromPortalItem({ portalItem: item }).then((layer) => { viewElement.map.add(layer); viewElement.extent = item.extent; }); } })});Please refer to the ArcGIS Organization portals for information on how the ArcGIS Maps SDK for JavaScript