čtvrtek 30. dubna 2009

How to access LN data

Few months ago I started to use PowerShell for access to my Lotus Notes Inbox. It is pretty cool because I don't need to go to LN every time the mail arrives and can see it directly in my dearest Win application:

You see I modified my profile so the number in brackets means how many emails I have in Inbox. Alias she calls my function Show-Email and displays last email (default behaviour). In this article I'd like to show you basic idea. In the next one will show you how to do the work more automatically. Thanks @ye110wbeard for asking on Twitter - it kicks me to finally write the article :)

Whole work with Lotus Notes consists of following steps:
  • Connect to Lotus Notes session
  • Select Database
  • Select View
  • Go thru all documents inside loop
Connect to Lotus Notes Session
As COM is used for connection, New-Object is the way:

PS C:\> $session = New-Object -ComObject Lotus.NotesSession
PS C:\> $session.Initialize()
PS C:\> $session.NotesVersion
Release 6.5.3|September 14, 2004

OK, we are in, let's continue.

Open database and select View
You can connect to database located on the server or to local database. In case of local database, use empty string as first parameter of GetDatabase method.

PS C:\> $db = $session.GetDatabase('','names.nsf')
PS C:\> $db.Title
Moravec's Address Book
PS C:\> $view = $db.GetView('Contacts')
PS C:\> $view.Name
PS C:\> $view.AllEntries.Count

You can see that I have 215 records in my Address book. I use AllEntries.Count every time I am downloading data from other (big) databases. I simply use the value in Write-Progress cmdlet so can see what's going on.

Grab data
Before you start do read records you have to know the names of items. Who is your best fried now? Of course Get-Member. Or you can go directly to LN database and check the fields manually via Document Properties.

PS C:\> $view.GetFirstDocument().Items | select Name | fw -col 4

Yupii - we are almost done:

PS C:\> $doc = $view.GetFirstDocument()
PS C:\> while ($doc -ne $null) {
$lastname = $doc.GetItemValue("LastName")
$firstname = $doc.GetItemValue("FirstName")
Write-Host "$firstname $lastname"
$doc = $view.GetNextDocument($doc)

You will see long list of names if you typed it correctly.

In this article I showed you how to easily connect to any LN database and read data from that db. I use the same technique for reading data from our company structure, converting it to objects and load it to PowerGUI for better work with the data.

Žádné komentáře: