Examples: DocumentContext property

This agent prints the value of the Subject field of the in-memory document.

  1. This agent displays the Subject item of the current document when run from a view in the Notes® client.
    import lotus.domino.*;
    
    public class JavaAgent extends AgentBase {
    
        public void NotesMain() {
    
            try {
              Session session = getSession();
              AgentContext agentContext = session.getAgentContext();
    
              // (Your code goes here) 
              Document doc = agentContext.getDocumentContext();
              System.out.println
                (doc.getItemValueString("Subject"));/
    
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
  2. This agent displays the Subject item of the current document when run by @Command([RunAgent]) in a browser.
    import lotus.domino.*;
    import java.io.PrintWriter;
    
    public class JavaAgent extends AgentBase {
    
        public void NotesMain() {
    
            try {
              Session session = getSession();
              AgentContext agentContext = session.getAgentContext();
    
              // (Your code goes here)
              Document doc = agentContext.getDocumentContext();
              PrintWriter pw = getAgentOutput();
              pw.println("<H2>" + doc.getItemValueString("Subject") +
                "</H2>");
    
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
  3. This agent displays the CGI variable Remote_Addr when run in a browser by the OpenAgent URL command, or by @Command([ToolsRunMacro]) if Remote_Addr is a field on the form supporting the current document.
    import lotus.domino.*;
    import java.io.PrintWriter;
    
    public class JavaAgent extends AgentBase {
    
        public void NotesMain() {
    
            try {
              Session session = getSession();
              AgentContext agentContext = session.getAgentContext();
    
              // (Your code goes here) 
              Document doc = agentContext.getDocumentContext();
              System.out.println(doc.getItemValueString("Remote_Addr"));
              PrintWriter pw = getAgentOutput();
              pw.println("<H2>IP address is " + 
                doc.getItemValueString("Remote_Addr") + "</H2>");
    
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
  4. This agent increments a counter. You can use it to track the number of times a Web page is opened by calling it from WebQueryOpen. The counter is maintained in a profile document as an item named Access_counter. An item of the name exists for display purposes only in the document being opened.
    import lotus.domino.*;
    
    public class JavaAgent extends AgentBase {
    
        public void NotesMain() {
    
            try {
              Session session = getSession();
              AgentContext agentContext = session.getAgentContext();
    
              // (Your code goes here) 
              Document doc = agentContext.getDocumentContext();
              Database db = agentContext.getCurrentDatabase();
              Document profile =
                db.getProfileDocument("(AccessCounterProfile)", null);
              String numStr = 
                profile.getItemValueString("Access_counter");
              String numStr2;
              if (numStr.equals(" ") || numStr.equals("")) {
                  numStr2 = new String("1");
                    }
              else {
                  Integer numInt = new Integer(numStr);
                  int num = numInt.intValue() + 1;
                  Integer numInt2 = new Integer(num);
                  numStr2 = new String(numInt2.toString());
                  }
                
              profile.replaceItemValue("Access_counter", numStr2);
              profile.save(false, false);
              doc.replaceItemValue("Access_counter", numStr2);
    
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }