I used sample Print Widget template and set the proxy to login the ArcGIS online Feature service. That's one is loading properly on the map but failed Print Service.
var permitUrl = "https://services8.arcgis.com/xxxx/arcgis/rest/services/xxxx/FeatureServer/0";
If I add the token manual then print service is working. Not sure why manual need to set the token for a print widget. Again this only for few layers, not all feature services.
var permitUrl = "https://services8.arcgis.com/xxxx/arcgis/rest/services/xxxx/FeatureServer/0?token=xxxxxxxxxxxx";
Snippet code of Print Widget. Let me know, did I make any mistake.
<script>
var app = {};
require([
"esri/map", "esri/layers/FeatureLayer",
"esri/dijit/Print", "esri/tasks/PrintTemplate", "esri/tasks/PrintTask",
"esri/request", "esri/config",
"dojo/_base/array", "dojo/dom", "dojo/parser", "esri/urlUtils",
"dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dojo/domReady!"
], function (
Map, FeatureLayer,
Print, PrintTemplate, PrintTask,
esriRequest, esriConfig,
arrayUtils, dom, parser, urlUtils
) {
parser.parse();
app.printUrl = "https://xxxx/arcgis/rest/services/xxx/GPServer/Export%20Web%20Map";
urlUtils.addProxyRule({
urlPrefix: "services8.arcgis.com/xxxx/arcgis/", //Server URL
proxyUrl: "proxy.ashx" // Path of Proxy page mine is avaiable on the root folder
});
esriConfig.defaults.io.proxyUrl = "proxy.ashx";
app.map = new esri.Map("map", {
basemap: "hybrid",
center: [-85.797466, 38.120622],
zoom: 17,
slider: false
});
// add graphics for pools with permits
var permitUrl = "https://services8.arcgis.com/xxxx/arcgis/rest/services/xxxx/FeatureServer/0";
//var permitUrl = "https://services8.arcgis.com/xxxx/arcgis/rest/services/xxxx/FeatureServer/0?token=xxxxxxxxxxxx";
var poolFeatureLayer = new FeatureLayer(permitUrl);
app.map.addLayer(poolFeatureLayer);
// get print templates from the export web map task
var printInfo = esriRequest({
"url": app.printUrl,
"content": { "f": "json" }
});
printInfo.then(handlePrintInfo, handleError);
function handlePrintInfo(resp) {
var layoutTemplate, templateNames, mapOnlyIndex, templates;
layoutTemplate = arrayUtils.filter(resp.parameters, function (param, idx) {
return param.name === "Layout_Template";
});
if (layoutTemplate.length === 0) {
console.log("print service parameters name for templates must be \"Layout_Template\"");
return;
}
templateNames = layoutTemplate[0].choiceList;
// remove the MAP_ONLY template then add it to the end of the list of templates
mapOnlyIndex = arrayUtils.indexOf(templateNames, "MAP_ONLY");
if (mapOnlyIndex > -1) {
var mapOnly = templateNames.splice(mapOnlyIndex, mapOnlyIndex + 1)[0];
templateNames.push(mapOnly);
}
// create a print template for each choice
templates = arrayUtils.map(templateNames, function (ch) {
var plate = new PrintTemplate();
plate.layout = plate.label = ch;
plate.format = "PDF";
plate.layoutOptions = {
"authorText": "Made by: Esri's JS API Team",
"copyrightText": "<copyright info here>",
"legendLayers": [],
"titleText": "Pool Permits",
"scalebarUnit": "Miles"
};
return plate;
});
// create the print dijit
app.printer = new Print({
"map": app.map,
"templates": templates,
url: app.printUrl
}, dom.byId("print_button"));
app.printer.startup();
}
function handleError(err) {
console.log("Something broke: ", err);
}
});
</script>