Select to view content in your preferred language

Can't save a webmap with code from sandbox example using seperate custom Javascript file

806
4
Jump to solution
08-25-2023 03:16 PM
dtstopper
Emerging Contributor

Hello,

I'm trying to save a webmap to my portal but I get the error below when trying to save it.

Error: The item does not exist yet and cannot be updated

I've tried both the update and save new code and neither works.  My sample code is below.

I have tried both a dynamic and hard coded link.  


// item automatically casts to a PortalItem instance by saveAs
const l_title = document.getElementById("webMapTitle");
const item = {
title: l_title.value
};

//// Setting authMode to immediate signs the user in once loaded
//portal = new Portal();
//portal.authMode = "immediate";
//// Auto should sign you in automatically
//portal.authMode = "auto";


// Update properties of the WebMap related to the view.
// This should be called just before saving a webmap.
//portal.load().then(function () {
map.updateFrom(view).then(() => {
map.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}`; //OG itemPageURL setting
//const itemPageUrl = "https://www.arcgis.com/home/item.html?id=a24a038265eb4f3584451292c70d68ad
const link = `<a target="_blank" href="${itemPageUrl}">${l_title.value}</a>`;

alert("Successfully saved as <i>${link}</i>");
})
// Save didn't work correctly
.catch((error) => {
if (error.name != "identity-manager:user-aborted") {
alert("Error: " + error.message);
}
});
});

 

 

I've also tried the to save using a portal also

////Save a new Webmap
portal.load().then(() => {
 map.updateFrom(view).then(() => {
 map.saveAs({
 title: l_title,
 portal: portal
 });
 });
});

 

Can anyone provide a working example of how to save to one's portal or get the portal to queue a log in window for me to authenticate?  It works when I query portal items, but now when I try to save maps.  The sanbox works on the ESRI server, but not in my local code.

Any help is appreciated, thank you!

0 Kudos
1 Solution

Accepted Solutions
dtstopper
Emerging Contributor

There was no issue with the code, it turns out that when you assign an API Key to your code when trying to save a map it will not work and give you a misleading error message saying the item doesn't exist.  If you are using an API Key from ESRI in your code, remove it from your map creation and saving code.  This resolved it for me, hope this helps someone else.

View solution in original post

0 Kudos
4 Replies
UndralBatsukh
Esri Regular Contributor

Hi there, 

Hopefully I understand your question right. To save a webmap to your portal you must specify the portal url on your portal item before calling webmap.saveAs as shown below: 

 

const item = {
   title: title.value,
   portal: {
     url: "url-to-your-portal"
   }
};

 

0 Kudos
dtstopper
Emerging Contributor

Hello,

I've tried 2 different portal URLs and both give me the same error.

Error: The item does not exist yet and cannot be updated

I've tried both URLs below with no luck.  I tried them seperately, but listed them both below to show what I'm using.

const itemPageUrl = https://sbcounty.maps.arcgis.com/

const itemPageUrl = https://www.arcgis.com/

// item automatically casts to a PortalItem instance by saveAs
const l_title = document.getElementById("webMapTitle");
const item = {
title: l_title.value,
portal: {
url: itemPageUrl
}
};

Do you have an example of a proper URL setting?

 

Also, I'm trying to use an authMode of "immediate" so it prompts me for my credentials, but that isn't happening. 

portal = new Portal();
portal.authMode = "immediate";

I just get the immediate error above.

I'm trying to save a new map to my profile as well as update an existing map I made from before.  Both examples don't work for me.

0 Kudos
dtstopper
Emerging Contributor

After further investigation, I found that the map and view objects I constructed are fine.  However, when the objects are being passed to my javascript code (in a seperate JS file) the objects are empty as opposed to running javscript inline with the HTML.  The sandbox example works perfectly when I copy and paste it into an html file, however I want to make it more dynamic by assembling the save functionality after a custom map has been rendered.  I will continue to look and figure out why it matters when you put this code in a JS file versus inline with HTML.

0 Kudos
dtstopper
Emerging Contributor

There was no issue with the code, it turns out that when you assign an API Key to your code when trying to save a map it will not work and give you a misleading error message saying the item doesn't exist.  If you are using an API Key from ESRI in your code, remove it from your map creation and saving code.  This resolved it for me, hope this helps someone else.

0 Kudos