Link to home
Start Free TrialLog in
Avatar of badtzmaru
badtzmaru

asked on

Writing to address book via java agent

I would be eternally grateful if there's someone out there who knows how to  WRITE to an address book database 'names.nsf' or write to database full stop.
Avatar of sduncan99
sduncan99

Can you give more details on what you are trying to do and what version of the server you are running
Avatar of badtzmaru

ASKER

lotus domino 5

i am trying to extract a value from a notes address book(for eg. FirstName) to be used with a java agent.

also i would be needing to write a value to the adderss book too
actually.. the whole point of me trying this is to import & export vCards to/from the address book
Can I ask why you are doing it with Java?
Can I ask why you are doing it with Java?
its for a university assignment.... i have to use object oriented java.
Hi badtzmaru,

This assumes R5 is being used and the class is being used within a Notes agent.

The lotus.domino.* classes are used to access the properties and methods of notes.

as with Notes itself, once you have access to the session, you can get access to all the appropriate notes methods and properties.

I have not been able to test this since I no longer have access to Java or R5.

Hope it helps anyway,

Scott.


import lotus.domino.*;

public class OpenNAB extends AgentBase
{
   public void NotesMain ()
   {
       Session sess = null;
       AgentContext agContext = null;
       int p;
       
       try
       {
           sess = getSession();
           agContext = sess.getAgentContext();
           Database dbNames = sess.getDatabase(null,"names.nsf");
           if (dbNames.isOpen())
               {
                 View vwUsers = dbNames.getView("($Users)");
                 Document doc = vwUsers.getFirstDocument();
                 while (doc!=null)
                       {
                           'Perform the action you need to on the document
                           'Get next document
                           doc = vwUsers.getNextDocument();
                       }
           }
           else
               {
                  System.out.println("names.nsf could not be opened");
           }    
       }
       catch (NotesException e)
       {
           System.err.println("NotesException: "+e.text);
           e.printStackTrace();
       }
       catch (Exception e)
       {
           e.printStackTrace();
       }  
   }
}    
This is a sample code to read & write a value to the 'FirstName' field in the person doc in the names.nsf personal address book on the local machine. You can follow up the output in the Java debug window.


import lotus.domino.*;
import java.io.*;
import java.util.*;

public class JavaAgent extends AgentBase {

      public void NotesMain() {

            try {
                  Session session = getSession();
                  AgentContext agentContext = session.getAgentContext();
                  System.out.println("Agent Started");
                  //Database db = session.getDatabase(null,"names.nsf");
                  Database db = agentContext.getCurrentDatabase();
                  View view = db.getView("Contacts");
                  Document doc = view.getFirstDocument();
                  while(doc != null) {
                        String fname = doc.getItemValueString("FirstName");
                        System.out.println(fname);
                                doc.replaceItemValue("FirstName","test value");
                        doc.save(true,false);
                        System.out.println("Doc Saved");
                        }
                        doc = view.getNextDocument(doc);
                  }
            } catch(Exception e) {
                  e.printStackTrace();
            }
      }
}

-Gus
ASKER CERTIFIED SOLUTION
Avatar of ghassan99
ghassan99

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial