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.