Count Pages in document via API stopped working

Following Code stopped working after moving to a new Server:

var xhr = new ActiveXObject("Microsoft.XmlHttp");
var setID = Watch.ExpandString("GetMeta(_vger_contentset_id[0], 10, Job.Group[0])")
xhr.open("GET","http://localhost:9340/rest/serverengine/entity/contentsets/"+setID+"/pages?detail=true",false,"user","password");
xhr.send();
var totalPages = 0;
if(xhr.status==200){
  var job = JSON.parse(xhr.responseText);
  var totalDocuments = job.length;
  for(var i=0;i<totalDocuments;i++){
    totalPages+=job[i].pages[0].count;
  }
}
Watch.Log("Documents : "+totalDocuments,2);
Watch.Log("Pages     : "+totalPages,2);

I can't find anything wrong there.

regards Ralf.

Hello Ralf,

Can you let us know please what you mean by “stopped working”? Does a specific error occur by executing the provided JavaScript code by a Run Script Workflow plugin, for example?

Tip: I would advice to declare the variable totalDocuments outside the if-statement instead, as shown in the below modified version of the shared script.

// ...
var totalPages = 0;
var totalDocuments = 0;

// ...
if (xhr.status === 200) {
	// ...
	totalDocuments = job.length;
	// ...
}
// ...

Looks like the localhost loopback is not enabled on your new server. You can test that theory by changing localhost to 127.0.0.1.

If that works, then you can simply use that IP address, but if you absolutely want to use the localhost name, you can follow the procedure outlined in this Microsoft Learn article.

2 Likes

Phil,
you solved it. Just changing localhost (even if ping localhost was valid) solved that issue.
Thx a lot!

Ralf.

Something strange still happens:
when using the debugger the rest api call works but running a job through the workflow I got:

11:53:09.975 [0014] Plugin Druckauftrag erstellen completed successfully - 11:53:09 (elapsed time: 00:00:03.729)
11:53:09.975 [0015] Run embedded script…
11:53:09.995 [0015] Include file not found, skipping:

thus resulting to 0 values.

Can you please share the code you have applied at the moment?

Sure:

var xhr = new ActiveXObject("Microsoft.XmlHttp");
var setID = Watch.ExpandString("GetMeta(_vger_contentset_id[0], 10, Job.Group[0])")
xhr.open("GET","http://127.0.0.1:9340/rest/serverengine/entity/contentsets/"+setID+"/pages?detail=true",false,"******","*********");
xhr.send();
var totalPages = 0;
if(xhr.status==200){
  var job = JSON.parse(xhr.responseText);
  var totalDocuments = job.length;
  for(var i=0;i<totalDocuments;i++){
    totalPages+=job[i].pages[0].count;
  }
}
Watch.Log("Documents : "+totalDocuments,2);
Watch.Log("Pages     : "+totalPages,2);

That message is displayed by the scripting task when one of the JS Includes cannot be found or loaded. It’s only a warning, however, because not all scripts use all Includes all the time. Since your script doesn’t appear to be using any external modules, I don’t believe this warning is the cause for your particular issue.

However, you should still investigate why you get that warning because it might be a sign that your setup is not longer correct, which means previous tasks may have been unable to perform their job properly, which could account for this script failing.

strange, all other tasks perform well,
the previus task is
grafik
“Create Job”
Maybe it’s due to “Enhanced JScript” Setting?

The Include file not found warning is an inconsequential known issue and can be ignored. It will be fixed in the next version of Workflow.

@fortiny : What’s to fix? Shouldn’t the scripting task always warn when an Include file cannot be loaded?

Yes, but here the problem is that it considers an empty array, or rather an array with an empty string, that is incorrectly left by the installation process. Hence the message skipping: followed by nothing.

I’ll retry after the installation of the current update.
Very strange…
Sometimes if I send the same job a second time it works, sometimes not.

Ahhh… makes perfect sense, now.