Examples: Accessing the Domino® Objects through COM

  1. This Visual Basic code initializes a Domino® session and prints the common name of the user ID.
    Private Sub InitializeDimNew_Click()
    Dim s As New NotesSession
    Call s.Initialize
    MsgBox s.CommonUserName, , "Common user name"
    End Sub
  2. This Visual Basic code initializes a Domino® session as shown previously, but uses CreateObject instead of New on NotesSession. Note that "s" must be declared as type NotesSession for early binding.
    Private Sub InitializeCreateObject_Click()
    Dim s As NotesSession
    Set s = CreateObject("Lotus.NotesSession")
    Call s.Initialize
    MsgBox s.CommonUserName, , "Common user name"
    End Sub
  3. This VBScript code initializes a Domino® session as shown previously.
    <HTML>
    <HEAD>
    <TITLE>Initialize Session</TITLE>
    <SCRIPT LANGUAGE="VBScript">
    Sub Button1_OnClick
        dim s
        Set s = CreateObject("Lotus.NotesSession")
        Call s.Initialize
        MsgBox s.CommonUserName,, "Common user name"
    End Sub
    </SCRIPT>
    </HEAD>
    <BODY>
    <H3>Initialize Session and Print User Name</H3><HR>
    <FORM>
    <INPUT NAME="Button1" TYPE="BUTTON" VALUE="Click Here">
    </FORM>
    </BODY>
    </HTML>
  4. This JScript code initializes a Domino® session as shown previously.

    <script language="JScript">
    var s;
    s = new ActiveXObject("Lotus.NotesSession");
    s.Initialize();
    document.write(s.CommonUserName);
    </script>
  5. This Visual Basic code prints the titles of all the databases on a Domino® server.
    Private Sub DbDirectory_Click()
    Dim s As New NotesSession
    Dim dir As NotesDbDirectory
    Dim db As NotesDatabase
    s.Initialize
    Set dir = s.GetDbDirectory("Doc/CAM/Notes")
    Set db = dir.GetFirstDatabase(NOTES_DATABASE)
    dbString = db.Title
    dbCount = 1
    While Not (db Is Nothing)
        If dbCount > 12 Then
            MsgBox dbString, , "Databases on " + dir.Name
            dbString = db.Title
            dbCount = 1
        Else
            dbString = dbString + Chr(10)
            dbString = dbString + db.Title
            dbCount = dbCount + 1
        End If
        Set db = dir.GetNextDatabase
    Wend
    MsgBox dbString, , "Databases on " + dir.Name
    End Sub
  6. This VBScript code prints the titles of all the databases on a Domino® server as shown previously.
    <HTML>
    <HEAD>
    <TITLE>Initialize Session</TITLE>
    <SCRIPT LANGUAGE="VBScript">
    Sub Button1_OnClick
        dim s, dir, db
        Set s = CreateObject("Lotus.NotesSession")
        Call s.Initialize
        Set dir = s.GetDbDirectory("Doc/CAM/Notes")
        Set db = dir.GetFirstDatabase(1247)
        dbString = db.Title
        dbCount = 1
        While Not (db Is Nothing)
            If dbCount > 12 Then
                MsgBox dbString,, "Databases on " + dir.Name
                dbString = db.Title
                dbCount = 1
            Else
                dbString = dbString + Chr(10)
                dbString = dbString + db.Title
                dbCount = dbCount + 1
            End If
            Set db = dir.GetNextDatabase
        Wend
        MsgBox dbString,, "Databases on " + dir.Name
    End Sub
    </SCRIPT>
    </HEAD>
    <BODY>
    <H3>Print Databases on Doc</H3><HR>
    <FORM>
    <INPUT NAME="Button1" TYPE="BUTTON" VALUE="Click Here">
    </FORM>
    </BODY>
    </HTML>
  7. This ASP code is similar to the last two examples. It prints the titles of all the databases on the current server.
    <%@ Language=VBScript %>
    <HTML>
    <HEAD>
    <META NAME="; GENERATOR; " Content="; Microsoft; Visual; Studio; 6#; ">
    </HEAD>
    <BODY>
    <%
        dim s, dir, db
        Set s = CreateObject("Lotus.NotesSession")
        Call s.Initialize
        dim serverName
        serverName = ""
        Set dir = s.GetDbDirectory(serverName)
        Set db = dir.GetFirstDatabase(1247)
        if (dir.Name <> "") then
           Response.Write("<h2>" + "ON " + dir.Name + "</h2>")
        else
            Response.Write("<h2> ON local</h2>")
        end if
        Response.Write("<table border=1  bgcolor=#CCCCCC>")
        Response.Write("<tr>")
        Response.Write("<th>File Name")
        Response.Write("<th>DB Title")
        While Not (db Is Nothing)
            Response.Write("<tr><td>")
            Response.Write(db.FileName)
            Response.Write("</td>")
            Response.Write("<td>")
            Response.Write(db.Title)
            Response.Write("</td></tr>")
            Set db = dir.GetNextDatabase
        Wend
        Response.Write("</table>")
    %>
    </BODY>
    </HTML>
  8. This Visual Basic code gets all the documents in a view of a Domino® database, and prints the values of a text item and multi-value text item.
    Private Sub DisplayItemValues_Click()
    Dim s As New NotesSession
    Dim db As NotesDatabase
    Dim v As NotesView
    Dim vn As NotesViewNavigator
    Dim e As NotesViewEntry
    Dim doc As NotesDocument
    Call s.Initialize
    Set db = s.GetDatabase("", "OverviewExamples.nsf")
    Set v = db.GetView("All Documents")
    Set vn = v.CreateViewNav()
    Set e = vn.GetFirstDocument()
    While Not (e Is Nothing)
        Set doc = e.Document
        subj = doc.GetFirstItem("Subject").Values
        MsgBox subj(0), , "Subject"
        cats = doc.GetFirstItem("Categories").Values
        For Each cat In cats
            MsgBox cat, , "Category"
        Next
        Set e = vn.GetNextDocument(e)
    Wend
    End Sub
  9. This ASP code is similar to the last example. It prints the values of a text item and multi-value date/time item.
    <%@ Language=VBScript %>
    <HTML>
    <HEAD>
    <META NAME="; GENERATOR; " Content="; Microsoft; Visual; Studio; 6#; ">
    </HEAD>
    <BODY>
    <%
        Dim s 'As New NotesSession
        Dim db 'As NotesDatabase
        Dim v 'As NotesView
        Dim vn 'As NotesViewNavigator
        Dim e 'As NotesViewEntry
        Dim doc 'As NotesDocument
        Set s = CreateObject("Lotus.NotesSession")
        Call s.Initialize
        Dim serverName
        serverName = ""
        Set db = s.GetDatabase(serverName, "AllTypes.nsf")
        Set v = db.GetView("All Documents")
        Set vn = v.CreateViewNav()
        Set e = vn.GetFirstDocument()
        While Not (e Is Nothing)
            Set doc = e.Document
            Dim values, item1
            Set item1 = doc.GetFirstItem("TextField")
            values = item1.Values
            Response.Write("<p>Subject: " + values(0) + "<br>")
            Set item1 = doc.GetFirstItem("DateTimeField")
            values = item1.Values 
            For Each dateTime In values
                Response.Write("DateTime: " + CStr(dateTime))
            Next
            Set e = vn.GetNextDocument(e)
        Wend
        
    %>
    </BODY>
    </HTML>
  10. This JScript code gets the value an item using the NotesDocument method GetItemValue. The array returned by GetItemValue requires special handling.

    <script language="JScript">
    var s;
    var dbdir;
    var db;
    var view;
    var doc;
    var forms = new Array(256);
    s = new ActiveXObject("Lotus.NotesSession");
    s.Initialize();
    dbdir = s.getDbDirectory("");
    db = dbdir.openDatabase("names");
    view = db.getView("Groups");
    doc = view.getFirstDocument();
    forms = VBArray(doc.GetItemValue("Form")).toArray();
    document.write(forms[0]);
    </script>
  11. This Visual Basic code prints a specific message if an anticipated error occurs. If another error occurs, the code prints the Notes® error code -- the last two bytes of the COM error code converted to an integer.
    Private Sub OpenDatabase_Click()
    Dim s As New NotesSession
    Dim dir As NotesDbDirectory
    Dim db As NotesDatabase
    Call s.Initialize
    Set dir = s.GetDbDirectory("")
    On Error GoTo errorHandler
    Set db = dir.OpenDatabase(Form1.DatabaseName.Text)
    MsgBox db.Title, , "Title"
    Exit Sub
    errorHandler:
    If Err() = ERR_SYS_FILE_NOT_FOUND Then
        MsgBox "Change the file name and try again", , _
        "File not found"
    Else
        MsgBox "Error " & _
        CInt("&H" & Right(Hex(Err()), 4)), , _
        "Domino error"
    End If
    End Sub
  12. This VBScript code prints a specific message if an anticipated error occurs. The error is defined by appending the hexadecimal value of the Domino® error code to &H80040 (4003 is 3 digits in hexadecimal). If another error occurs, the code prints the Domino® error code -- the last two bytes of the COM error code converted to an integer.
    Sub Button1_OnClick
        On Error Resume Next
        dim s, dir, db, ERR_SYS_FILE_NOT_FOUND
        If Len(Hex(4003)) = 3 Then
            ERR_SYS_FILE_NOT_FOUND = Clng("&H80040" & Hex(4003))
        Else
            ERR_SYS_FILE_NOT_FOUND = Clng("&H8004" & Hex(4003))
        End If
        Set s = CreateObject("Lotus.NotesSession")
        Call s.Initialize
        Set dir = s.GetDbDirectory("")
        Set db = dir.OpenDatabase(Document.Form1.Text1.Value)
        If Err.Number = 0 Then
            MsgBox db.Title,, "Title"
        Else
            If Err.Number = ERR_SYS_FILE_NOT_FOUND Then
                MsgBox "Change the file name and try again",, _
                "File not found"
            Else
                MsgBox CInt("&H" & Right(Hex(Err.Number),4)),, _
                    "Domino error"
            End If
        End If
    End Sub
  13. This ASP code prints the results of a full-text search.
    <%@ Language=VBScript %>
    <HTML>
    <HEAD>
    <META NAME="; GENERATOR; " Content="; Microsoft; Visual; Studio; 6#; ">
    </HEAD>
    <BODY>
    <%
    'URL http://yourserver/Demo9.asp?key=keyWord
        Dim s 'As New NotesSession
        Dim db 'As NotesDatabase
        Dim v 'As NotesView
        Dim vn 'As NotesViewNavigator
        Dim e 'As NotesViewEntry
        Dim doc 'As NotesDocument
        Set s = CreateObject("Lotus.NotesSession")
        Call s.Initialize
        Dim serverName
        serverName = ""
        Response.Write("<h2>Full Text Search Result</h2>")
        Dim dbName, keyWord
        keyWord = Request.QueryString("key")
        dbName = "employees.nsf"
        Set db = s.GetDatabase(serverName, dbName)
        If (db is Nothing) then
            Response.Write("<p>Cannot find the database: " + dbName)
        Else
            Response.Write("<p>Database: " + db.FileName + "<br>")
            Response.Write("Key Word: " + keyWord + "<br>")
        End if
        Call db.UpdateFTIndex(true)    
        Set dc = db.FTSearch(keyWord, 100)
        Response.Write("Records: " + CStr(dc.Count) + "<br><br><br>")
        Set doc = dc.GetFirstDocument()
        Dim thisItem
        If Not (doc is Nothing) then
            Response.Write("<table border=1 bgcolor=#CCCCCC>")
            Response.Write("<th>ID")
            Response.Write("<th>First Name")
            Response.Write("<th>Last Name")
            Response.Write("<th>Department")
            Response.Write("<th>Title")
        End if
        While Not (doc Is Nothing)
            Response.Write("<tr>")
            Set thisItem = doc.GetFirstItem("ID")
            Response.Write("<td>" + thisItem.Text + "</td>")
            Set thisItem = doc.GetFirstItem("FirstName")
            Response.Write("<td>" + thisItem.Text + "</td>")
            Set thisItem = doc.GetFirstItem("LastName")
            Response.Write("<td>" + thisItem.Text + "</td>")
            Set thisItem = doc.GetFirstItem("Department")
            Response.Write("<td>" + thisItem.Text + "</td>")
            Set thisItem = doc.GetFirstItem("Title")
            Response.Write("<td>" + thisItem.Text + "</td>")
            Set doc = dc.GetNextDocument(doc)
            Response.Write("</tr>")
        Wend
        Response.Write("</table>")
        
    %>
    </BODY>
    </HTML>

    Below is an HTML file to drive the ASP:

    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE></TITLE>
    </HEAD>
    <BODY>
    <form method=GET  action="http://mkoma4/COM_AspSep28/Demo/Demo9.asp">
    <h2>Full Text Search on Employees Database</h2>
    <br>
    <br>
    <P>Input A Keyword: 
    <input name=key size=20>
    <br>
    <br>
    <br>
    <input type=reset value="Clear Form">
    <input type=submit value="Submit">
    
    </form>
    </BODY>
    </HTML>