Getting to Portal Item in JSAPI 3.x

773
1
09-23-2020 12:58 PM
Arne_Gelfert
Occasional Contributor III

I was working on some webmap JSON massaging piece of code and wanted to get to the JSON of a Portal item. Looks like 4.x has a PortalItem class. But the closest I could there with 3.x was this:

define([...,
"esri/arcgis/Portal",
"esri/request"
],
function(...,
arcgisPortal,
esriRequest)}

...

let portal = new arcgisPortal.Portal('https://my-super-awesome-portal');
let itemParams = {
  q: 'id: gobbledeegobbledeegook'
};
       
portal.queryItems(itemParams)
.then(function(result){

  let itemDataUrl = result.results[0].itemDataUrl;
  let request = esriRequest({
    
            url: itemDataUrl,
            content: {f: 'json'},
            handleAs: 'json',
            
});
             
request.then(function(response){

    let operationalLayersJSON = response['operationalLayers'];

    console.log(JSON.stringify(operationalLayersJSON));

      }, function(error) {
      console.log("Error: ", error.message);
});

});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Given the differences between 3.x/4.x, is the above the best way to do it? It works. Of course, it relies on me knowing and plugging the item's gobbledeegook ID.

If I'm working in WAB and the item of interest is the current app's  Portal web map, then I can use:

this.map.itemId
0 Kudos
1 Reply
RobertScheitlin__GISP
MVP Emeritus

Arne,

  That way is just fine. There is ways to work with Portal items in 3.x as well though.

Portal | API Reference | ArcGIS API for JavaScript 3.33 

The Portal class allows for querying items to find that portal item id.

0 Kudos