Select to view content in your preferred language

setRequestPreCallback on PrintTask

5052
5
Jump to solution
02-05-2014 08:34 AM
BenFousek
Deactivated User
I'm having a brain block this morning and having problems using setRequestPreCallback with PrintTask.

Thanks.
0 Kudos
1 Solution

Accepted Solutions
BenFousek
Deactivated User
Got it. And figured out how to reset setRequestPreCallback so it doesn't fire on other requests.
esriRequest.setRequestPreCallback(function (args) {     //do stuff     return args; }); this.printTask.execute(this.printParameters, lang.hitch(this, this._print), lang.hitch(this, this._error)); //reset esriRequest.setRequestPreCallback();

View solution in original post

0 Kudos
5 Replies
BenFousek
Deactivated User
Got it. And figured out how to reset setRequestPreCallback so it doesn't fire on other requests.
esriRequest.setRequestPreCallback(function (args) {     //do stuff     return args; }); this.printTask.execute(this.printParameters, lang.hitch(this, this._print), lang.hitch(this, this._error)); //reset esriRequest.setRequestPreCallback();
0 Kudos
BenFousek
Deactivated User
Here's some follow up for interested parties.

Until yesterday I'd never really used PrintTask or associated classes, instead creating my own Web_Map_as_JSON and making a independent request for a couple reasons:
1) I didn't want to print all layers, e.g. a results layer or custom gp layer.
2) Sending attribute and info template json can exponentially increase the size of the request and with no benefit. And cause errors, especially with text symbols.

This approach has worked well, but has required a lot more coding and code maintenance as dojo/jsapi have evolved. Wanting to reduce code and let the native esri classes do the heavy lifting, this is my 26 lines of code to get the same result. With the following understanding: 1) layers that are not to be printed are extended with layer._print = false;, 2) this example is out of a custom class (hence the hitching), and 3) this.map is in fact the map.

esriRequest.setRequestPreCallback(lang.hitch(this, function (args) {
    //Web_Map_as_JSON to js obj
    var json = JSON.parse(args.content.Web_Map_as_JSON);
    //remove map.graphics
    json.operationalLayers.splice(arrayUtils.indexOf(json.operationalLayers, this.map.graphics.id), 1);
    //remove non-print layers
    arrayUtils.forEach(json.operationalLayers, function (ol) {
        var layer = this.map.getLayer(ol.id);
        if (layer._print && layer._print === false) {
            json.operationalLayers.splice(arrayUtils.indexOf(json.operationalLayers, ol), 1);
        }
    }, this);
    //strip graphic layer attributes and and info templates
    arrayUtils.forEach(json.operationalLayers, function (ol) {
        if (ol.featureCollection && ol.featureCollection.layers.length) {
            arrayUtils.forEach(ol.featureCollection.layers, function (layer) {
                arrayUtils.forEach(layer.featureSet.features, function (feature) {
                    if (feature.attributes) {
                        delete feature.attributes;
                    }
                    if (feature.infoTemplate) {
                        delete feature.infoTemplate;
                    }
                });
            });
        }
    });
    //json to Web_Map_as_JSON
    args.content.Web_Map_as_JSON = JSON.stringify(json);
    return args;
}));


A final consideration. If you are using a custom print task server side and need attributes to do something cool, like create a table of attributes for a selected feature or label graphics, then steps would need to be taken not to strip those attributes from those layers.
SebastianRoberts
Frequent Contributor

This is excellent!  Exactly what I was looking for. 

One small change on line 9.  If the _print property is false then the if statement will still evaluate to false.  I think it should read:

if (layer.hasOwnProperty('_print') && layer._print === false) {

0 Kudos
SurendranNeelakantan
Occasional Contributor

It  looks setRequestPreCallback   is not available   in  the API version 4.x  (request | API Reference | ArcGIS API for JavaScript 4.3 )

I am trying to convert my  old print task  using  new 4.x API.   Is there any alternate way to retrieve and modify args.content.Web_Map_as_JSON   before it call print task?

0 Kudos
TravisButcher
Esri Contributor

It is implemented in the API just not documented yet.  You can refer back to the 3.x documentation for usage