Skip to content

Instantly share code, notes, and snippets.

@nommuna2
Last active April 26, 2019 21:27
Show Gist options
  • Save nommuna2/4caf4114fa44d197e0b3207c7682cd66 to your computer and use it in GitHub Desktop.
Save nommuna2/4caf4114fa44d197e0b3207c7682cd66 to your computer and use it in GitHub Desktop.
(ArcGIS API for JavaScript) Sample of getting user items and accessing secured layers without user authentication.
require(["esri/map",
"esri/arcgis/Portal",
"esri/urlUtils",
"esri/request",
"esri/config",
"dojo/domReady!"
], function (Map, arcgisPortal,urlUtils, Request, Config) {
//Push the server to the cors array to avoid CORS related errors.
Config.defaults.io.corsEnabledServers.push("FQDN of server");
var map = new Map("map", {
center: [-118, 34.5],
zoom: 8,
basemap: "topo"
});
//Add a proxy rule, to send requests through the proxy
urlUtils.addProxyRule({
urlPrefix: "FQDN of the web server which the proxy is on",
proxyUrl: "https://FQDN/DotNet/proxy.ashx"
});
var urls = [
"https://FQDN/portal/sharing/rest/content/users/admin", //See what files are associated with a certain user from portal.
"https://FQDN/server/rest/services/Hosted/Cites/FeatureServer" //Access a secured layer from portal.
];
//Send a request using the request module
urls.forEach((element) =>{
var request = new Request({
url: element,
content: {f:"pjson"},
handleAs: "json"
});
request.then((response) => { console.log(response);});
});
});
@nommuna2
Copy link
Author

nommuna2 commented May 7, 2018

This is a sample using the proxy to access items and secured layers from a portal. The use of a proxy allows you to store credentials in the proxy config so that the end user will not have to log in.

Additional information to be aware of:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment