Question: When upgrading classic Domino applications to XPages one particular problem arises constantly: "what to do with the PRINT statements in existing agents that write back directly to the browser?" I have 200 agents in my nsf file which all uses Print statement in their code.
I have investigated found the link <http://www.wissel.net/blog/d6plinks/SHWL-8SF7AH>
but it is not possible for me to change agent code as it is also used from forms.
I have to re-use these agents from xpages on button click and also have to pass document.
Is there any way or alternative which can solve my problem.
help is required.

We have Agent.runWithDocumentContext(doc:NotesDocument) method which can run the agent from xpage and passing an in-memory document. I create an im-memory document

```
   var doc = database.createDocument();
   doc.replaceItemValue("StartDate",startDate.getDateOnly())
   doc.replaceItemValue("EndDate",endDate.getDateOnly())
   doc.replaceItemValue("ReportName",reportName)
    var agent:NotesAgent = database.getAgent("("+reportName+")");

    agent.runWithDocumentContext(doc);   

```

This in-memory document i can pass to the agent. But the issue I am facing currently that my agents are used to print directly which is not possible through xpage i assume.
Is there any alternative way to pass in-memory document to agent and able to print directly to browser through xpages.
Help needed please help.

Answer: 
create a java class that calls your agent and read the output from your agents in an input stream and display the stream in computed field in Xpages. 

here is a sample java class that will retrive the output from your agent

```
package com.thomas;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

    public class getAgentData{

        public String getData() throws IOException {
            try {
                URL url = new URL("http://localhost/mydatabase.nsf/myagent?openagent");

                BufferedReader in = new BufferedReader(
                new InputStreamReader(url.openStream()));
                String content = new String();
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    content+=inputLine;
                }
                in.close();
                return content;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

```

If you add your java class as a bean in faces-config.xml you can call it using a computed field in your xpages like this 

```
<xp:text escape="true" id="computedField1" value="#{javascript:getAgentData().getData()}"></xp:text>

```
You need to change your agent a little. Instead of `Print "Something"` you would use `someobj.print "Something"` - which should be a manageable task using search/replace. The detailed steps have been documented [in this blog entry](http://www.wissel.net/blog/d6plinks/SHWL-8SF7AH).

Now you state that you need to dual use the agent. Not that hard, once you move beyond copy and paste programming.

When you check the class - it looks for documentcontext. When you call the agent directly, the document context is not there. So you check if you have the document - if not it is classic and you add a print statement to the class, if it is there you save to the context.

The code is `myObject.printResult = true` to print - or you set it to true per default (but then you pollute your log :-) )

If you provide a context doc when calling the agent, then you would provide an item (e.g. `DontPrint=1`) from your XPages and check that one. If it is there, only save to the context, otherwise add the print statement. 

Let us know how it goes.