This sample demonstrates how to save a WebScene to a Portal for ArcGIS item by either creating a new item or overwriting an existing item that has been loaded from a Portal for ArcGIS item.
Saving a web scene is easy. All that's required is a WebScene and a valid Portal to save the scene to.
Saving to a new item
Create a new empty WebScene instance and a Portal instance where the WebScene should be saved to. Loading the Portal will trigger user authentication, and if successful the item will be saved to the given Portal.
/************************************************************
* Creates a new WebScene instance. A WebScene can reference
* a PortalItem ID that represents a WebScene saved to
* arcgis.com or an on-premise portal.
*
* To load a WebScene from an on-premise portal, set the portal
* url with esriConfig.portalUrl (see above).
************************************************************/
const scene = new WebScene({
portalItem: {
// autocasts as new PortalItem()
id: "90e3c30bdbbe4886a94f75c2cdd7edce"
}
});
/************************************************************
* Set the WebScene instance to the map property in a
* SceneView.
************************************************************/
const view = new SceneView({
map: scene,
container: "viewDiv"
});
In case the scene is added to a view, the scene can only be saved once the view properties are updated on the scene:
// Update properties of the WebScene related to the view.
// This should be called just before saving a webscene.
scene.updateFrom(view).then(() => {
scene
.saveAs(item)
// Saved successfully
.then((item) => {
// link to the newly-created web scene item
const itemPageUrl = item.portal.url + "/home/item.html?id=" + item.id;
const link = '<a target="_blank" href="' + itemPageUrl + '">' + title.value + "</a>";
statusMessage("Save WebScene", "<br> Successfully saved as <i>" + link + "</i>");
})
// Save didn't work correctly
.catch((error) => {
statusMessage("Save WebScene", "<br> Error " + error);
});
});
Overwriting an existing item
Create a new WebScene instance and set the portal item ID inside the portalItem property of the WebScene. Loading the Portal will trigger user authentication, and if successful the item will be saved to the given Portal.
/************************************************************
* Creates a new WebScene instance. A WebScene can reference
* a PortalItem ID that represents a WebScene saved to
* arcgis.com or an on-premise portal.
************************************************************/
const scene = new WebScene({
portalItem: {
id: "3a9976baef9240ab8645ee25c7e9c096"
}
});
/************************************************************
* Create a new Portal instance and request immediate user
* authentication.
************************************************************/
const portal = new Portal({
authMode: "immediate"
});
/************************************************************
* Loading of either the scene or the portal will trigger
* authentication and once both Promises are resolved, the
* Webscene will be saved thereby overwriting the previously
* loaded PortalItem with any changes that have occurred.
************************************************************/
scene.load().then(() => {
portal.load().then(() => {
scene.portalItem.title = "Modified WebScene";
scene.portalItem.portal = portal;
scene.save();
});
});
To use load or save items from an on-premise portal, set the URL of the portal in esriConfig.portalUrl.
Please refer to the ArcGIS Organization portals for information on how the ArcGIS Maps SDK for JavaScript makes use of working with portal items.