<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Token update using removeLayer, addLayer. Is there a better way? in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507377#M47184</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Joel, Great job of sleuthing!&amp;nbsp; Perhaps ESRI will someday support a documented method to update token.&amp;nbsp; At least while version 3.12 is around&amp;nbsp; there is now an alternative to "new" layer.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 19 Feb 2015 18:27:56 GMT</pubDate>
    <dc:creator>BlairJones</dc:creator>
    <dc:date>2015-02-19T18:27:56Z</dc:date>
    <item>
      <title>Token update using removeLayer, addLayer. Is there a better way?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507367#M47174</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have reviewed many discussions on this forum where a desire is expressed to refresh access tokens using JavaScript. I have not found an elegant solution.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there a better way?&amp;nbsp; Just updating the .url property of a layer with a fresh token does not work.&amp;nbsp; A not very elegant way to change a token is to use method removeLayer followed by method addLayer with a url string that includes a fresh token ( many undesirable side effects ).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I want to manage access based on credentials entered on my login page.&amp;nbsp; On Windows platform running IIS, It is easy to get a fresh token string from a process running in the same IIS Session used by login page with all sensitive credentials safe as Session variables.&amp;nbsp; A fresh token string can easily be obtained by JavaScript in a subsequent map web page run in the same session with a new layer added using this token part of the url parameter.&amp;nbsp; What is surprisingly not easy is to update an existing map layer's access token.&amp;nbsp; I do not understand how to best do this using esri.map JavaScript API documentation.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Feb 2015 04:50:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507367#M47174</guid>
      <dc:creator>BlairJones</dc:creator>
      <dc:date>2015-02-12T04:50:39Z</dc:date>
    </item>
    <item>
      <title>Re: Token update using removeLayer, addLayer. Is there a better way?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507368#M47175</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Interesting you should ask since I was working on this yesterday.&amp;nbsp; Below you will find the essentials of my solution, and it appears to be working well with 3.12, although its elegance is debatable.&amp;nbsp; Note that "urlUtils" is esri/urlUtils - you'll have to figure out how best to work that module into your solution.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp; function setLayerTokenProperties(layer, token) {
&amp;nbsp;&amp;nbsp; if ((layer) &amp;amp;&amp;amp; (token)) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; layer.url = addTokenToURL(token, layer.url);
&amp;nbsp;&amp;nbsp;&amp;nbsp; layer._url = urlUtils.urlToObject(layer.url);
&amp;nbsp;&amp;nbsp;&amp;nbsp; layer._params.token = token;
&amp;nbsp;&amp;nbsp; }
&amp;nbsp; }
&amp;nbsp; function addTokenToURL(token, url) {
&amp;nbsp;&amp;nbsp; if ((url) &amp;amp;&amp;amp; (token)) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; var sIndex = url.indexOf("token=");
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (sIndex &amp;gt; 0) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var eIndex = url.indexOf("&amp;amp;", sIndex + 1);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (eIndex &amp;gt; sIndex)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return url.substring(0, sIndex) + "token=" + token + url.substr(eIndex);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return url.substring(0, sIndex) + "token=" + token;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } else if (url.indexOf("?") &amp;gt; 0)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return url + "&amp;amp;token=" + token;
&amp;nbsp;&amp;nbsp;&amp;nbsp; else
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return url + "?token=" + token;
&amp;nbsp;&amp;nbsp; } else
&amp;nbsp;&amp;nbsp;&amp;nbsp; return url;
&amp;nbsp; }&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:14:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507368#M47175</guid>
      <dc:creator>JoelBennett</dc:creator>
      <dc:date>2021-12-11T22:14:24Z</dc:date>
    </item>
    <item>
      <title>Re: Token update using removeLayer, addLayer. Is there a better way?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507369#M47176</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for the reply. urlUtils.urlToObject seems defined well enough in the API documentation.&lt;/P&gt;&lt;P&gt;I have not seen these properties described anywhere; where do you find documentation?&lt;/P&gt;&lt;OL class="dp-c" start="1"&gt;&lt;LI&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer._url&lt;/LI&gt;&lt;LI&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer._params&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I can't find any reference using web search.&amp;nbsp; Are these "special knowledge" things?&amp;nbsp; When one updates layer.url are these supposed to be updated automatically but are not being updated because of a bug? or are they updated in layer when method addLayer&amp;nbsp; is used?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'll spend a few hours testing these and then I'll report back.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Feb 2015 19:38:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507369#M47176</guid>
      <dc:creator>BlairJones</dc:creator>
      <dc:date>2015-02-12T19:38:07Z</dc:date>
    </item>
    <item>
      <title>Re: Token update using removeLayer, addLayer. Is there a better way?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507370#M47177</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK, reporting back...&amp;nbsp; setting those properties does not work for me.&amp;nbsp; Using Firefox browser.&amp;nbsp; I report the new token on my test page and assign those properties; but when I pan the map, the browser console shows new net references using the original token.&amp;nbsp; When I use removeLayer - AddLayer then the new net references show the new token.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Feb 2015 21:44:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507370#M47177</guid>
      <dc:creator>BlairJones</dc:creator>
      <dc:date>2015-02-12T21:44:18Z</dc:date>
    </item>
    <item>
      <title>Re: Token update using removeLayer, addLayer. Is there a better way?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507371#M47178</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Unfortunately I cannot explain why it works for me and not you - my map wouldn't even load if it didn't.&amp;nbsp; I'm using IE 11, but don't think that's the culprit.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The _url and _params properties are not documented.&amp;nbsp; I found them by going into the source code for the layer classes (Layer, DynamicMapServiceLayer, ArcGISDynamicMapServiceLayer, etc).&amp;nbsp; It's unorthodox, but I have to thoroughly test between each new version of the API anyways...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Feb 2015 23:39:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507371#M47178</guid>
      <dc:creator>JoelBennett</dc:creator>
      <dc:date>2015-02-12T23:39:07Z</dc:date>
    </item>
    <item>
      <title>Re: Token update using removeLayer, addLayer. Is there a better way?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507372#M47179</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Once such possibility that might be causing this not to work for you is if your layers use the credential property (which mine do not).&amp;nbsp; If so, updating the setTokenLayerProperties method like below may make the difference (although I haven't tested this):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;function setLayerTokenProperties(layer, token, expires) {
 if ((layer) &amp;amp;&amp;amp; (token)) {
&amp;nbsp; layer.url = addTokenToURL(token, layer.url);
&amp;nbsp; layer._url = urlUtils.urlToObject(layer.url);
&amp;nbsp; layer._params.token = token;
&amp;nbsp; if (layer.credentials) {
&amp;nbsp;&amp;nbsp; layer.credentials.token = token;
&amp;nbsp;&amp;nbsp; layer.credentials.expires = expires;
&amp;nbsp; }
 }
}&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:14:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507372#M47179</guid>
      <dc:creator>JoelBennett</dc:creator>
      <dc:date>2021-12-11T22:14:27Z</dc:date>
    </item>
    <item>
      <title>Re: Token update using removeLayer, addLayer. Is there a better way?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507373#M47180</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for additional ideas, but my test if(layer.credentials) comes up false.&lt;/P&gt;&lt;P&gt;I did not mention I am working with&amp;nbsp; layer = new FeatureLayer( url, options );&amp;nbsp;&amp;nbsp; Does your method work for you on layer constructed as FeatureLayer ?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 13 Feb 2015 19:29:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507373#M47180</guid>
      <dc:creator>BlairJones</dc:creator>
      <dc:date>2015-02-13T19:29:51Z</dc:date>
    </item>
    <item>
      <title>Re: Token update using removeLayer, addLayer. Is there a better way?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507374#M47181</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;My code there was incorrect...it should be "layer.credential" (singular), not "layer.credentials" (plural).&amp;nbsp; That's what I get for posting untested code...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Yes, what I originally posted works with instances of ArcGISDynamicMapServiceLayer, ArcGISTiledMapServiceLayer, ArcGISImageServiceLayer, and FeatureLayer.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 13 Feb 2015 19:36:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507374#M47181</guid>
      <dc:creator>JoelBennett</dc:creator>
      <dc:date>2015-02-13T19:36:38Z</dc:date>
    </item>
    <item>
      <title>Re: Token update using removeLayer, addLayer. Is there a better way?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507375#M47182</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am using initially&lt;/P&gt;&lt;P&gt;myLayer = new FeatureLayer(url+"?token="+initialToken,myOptions);&lt;/P&gt;&lt;P&gt;map.addLayer(myLayer);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have tested for mayLayer.credential which comes up false.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and later associated with setTimeout&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; myLayer.url = url + "?token=" + newToken;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; myLayer._url = urlUtils.urlToObject( myLayer.url );&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; myLayer._params.token = "" + newToken;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and then the browser console still shows net acgitvity to url using initialToken.&amp;nbsp; A new token is used only when I code&lt;/P&gt;&lt;P&gt;myLayer = new FeatureLayer(url+"?token="+newToken,myOptions);&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 16 Feb 2015 18:46:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507375#M47182</guid>
      <dc:creator>BlairJones</dc:creator>
      <dc:date>2015-02-16T18:46:24Z</dc:date>
    </item>
    <item>
      <title>Re: Token update using removeLayer, addLayer. Is there a better way?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507376#M47183</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I had to look into this further because it also wasn't working properly with my own FeatureLayers.&amp;nbsp; I've found that FeatureLayers have a _task property which is an instance of QueryTask.&amp;nbsp; This _task property is created when the FeatureLayer is initialized, and its URL never appears to get updated after that (as far as I can tell).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Anyhow, I've revised the function to below (and it appears to be working properly).&amp;nbsp; Nothing like building an airplane while you're flying it...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;function setLayerTokenProperties(layer, token, expires) {
 if ((layer) &amp;amp;&amp;amp; (token)) {
&amp;nbsp; layer.url = addTokenToURL(token, layer.url);
&amp;nbsp; layer._url = urlUtils.urlToObject(layer.url);
&amp;nbsp; layer._params.token = token;
&amp;nbsp; if (layer.credential) {
&amp;nbsp;&amp;nbsp; layer.credential.token = token;
&amp;nbsp;&amp;nbsp; layer.credential.expires = expires;
&amp;nbsp; }
&amp;nbsp; if ((layer.declaredClass == "esri.layers.FeatureLayer") &amp;amp;&amp;amp; (layer._task)) {
&amp;nbsp;&amp;nbsp; layer._task.url = layer.url;
&amp;nbsp;&amp;nbsp; layer._task._url = urlUtils.urlToObject(layer.url);
&amp;nbsp; }
 }
} &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:14:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507376#M47183</guid>
      <dc:creator>JoelBennett</dc:creator>
      <dc:date>2021-12-11T22:14:29Z</dc:date>
    </item>
    <item>
      <title>Re: Token update using removeLayer, addLayer. Is there a better way?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507377#M47184</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Joel, Great job of sleuthing!&amp;nbsp; Perhaps ESRI will someday support a documented method to update token.&amp;nbsp; At least while version 3.12 is around&amp;nbsp; there is now an alternative to "new" layer.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Feb 2015 18:27:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/token-update-using-removelayer-addlayer-is-there-a/m-p/507377#M47184</guid>
      <dc:creator>BlairJones</dc:creator>
      <dc:date>2015-02-19T18:27:56Z</dc:date>
    </item>
  </channel>
</rss>

