Simplifying IBM Notes Locations Using Lotusscript

Struggling to remember which location is the right one to select based on where you are located? In a quest to simplify the use of IBM Notes (formerly Lotus Notes) in our environment I started working on this piece of Lotusscript. Often people would struggle with issues receiving mail, it taking longer than it should for the mail to come through; 9 times out of 10 it was due to the fact that they selected the wrong location.

I created this script to remove all locations except Online and Offline, if Online doesn’t exist I create it, I have it set the current location to Online, then it deletes specific connection documents, and finally it adds two connection documents. I wanted the connection documents to only be processed if necessary so I set the priority to Low. It appears that the connection documents are handled alphabetically by destination server address if they are both set to priority low; I haven’t fully test this but it’s something to keep in mind.

Sub Click(Source As Button)
Dim db As New NotesDatabase( "", "names.nsf" )
Dim view As NotesView
Dim agent As NotesAgent
Dim doc As NotesDocument
Dim nextdoc As NotesDocument
Dim workspace As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim location As String

Dim session As New NotesSession
Dim mailDb As NotesDatabase
Dim mailDoc As NotesDocument
Dim rtItem As NotesRichTextItem
Set mailDb = session.CurrentDatabase

Set mailDoc = MailDb.CreateDocument
mailDoc.form = "memo"
mailDoc.sendto = "user/acme"
mailDoc.subject = "Lotusscript Location button was clicked"
Set rtItem = mailDoc.CreateRichTextItem("Body")

On Error Resume Next
On Error 4241 Goto BadDoc

'----------------------------------------------------------------------
'Section to Create New Location
'----------------------------------------------------------------------
'Search for Online location document
Set view = db.GetView("Locations")
Set doc = view.GetFirstDocument
While Not doc Is Nothing
LocName = doc.GetItemValue("Name")
Set nextdoc=view.GetNextDocument(doc)
If LocName(0) = "Online" Then
Call doc.ReplaceItemValue("LocationType", "0")
Call doc.ReplaceItemValue("MailServer", "acme/acme")
Call doc.ReplaceItemValue("MailType", "0")
Call doc.ReplaceItemValue("Domain", "acme")
Call doc.ReplaceItemValue("ExhaustiveNameLookup", "1")
Call doc.ReplaceItemValue("ReplicationEnabled", "1")
Call doc.ReplaceItemValue("Enabled", "1")
Call doc.ReplaceItemValue("Enabled_HI", "1")
Call doc.ReplaceItemValue("Schedule", "12:00:00 AM - 11:59:00 PM")
Call doc.ReplaceItemValue("Schedule_HI", "12:00:00 AM - 11:59:00 PM")
Call doc.ReplaceItemValue("Interval", "5")
Call doc.ReplaceItemValue("Interval_HI", "5")
Call doc.Save( True, True )

Call workspace.Locationsrefresh()
Call workspace.SetCurrentLocation("Online")

'Msgbox "Online Location already exists"
Call rtitem.AddNewLine(1)
Call rtItem.Appendtext("Online Location already exists")
Goto RemoveLocations
End If
Set doc=nextdoc
Wend

'If the Location does not exist, create it
Call workspace.ComposeDocument( "", "names.nsf", "Location" )
Set uidoc = workspace.Currentdocument
Call uidoc.FieldSetText("LocationType", "0")
Call uidoc.FieldSetText("MailServer", "acme/acme")
Call uidoc.FieldSetText("DirectoryServer", "acme/acme")
Call uidoc.FieldSetText("Name", "Online")
Call uidoc.FieldSetText("MailType", "0")
Call uidoc.FieldSetText("Domain", "acme")
Call uidoc.FieldSetText("ExhaustiveNameLookup", "1")
Call uidoc.FieldSetText("MailThreshold", "1")
Call uidoc.FieldSetText("ReplicationEnabled", "1")
Call uidoc.FieldSetText("Enabled", "1")
Call uidoc.FieldSetText("Enabled_HI", "1")
Call uidoc.FieldSetText("Schedule", "12:00:00 AM - 11:59:00 PM")
Call uidoc.FieldSetText("Schedule_HI", "12:00:00 AM - 11:59:00 PM")
Call uidoc.FieldSetText("Interval", "5")
Call uidoc.FieldSetText("Interval_HI", "5")
'Msgbox "Online Location created"
Call rtitem.AddNewLine(1)
Call rtItem.Appendtext("Online Location Created")

Call uidoc.Save
Call uidoc.Close

Call workspace.Locationsrefresh()
Call workspace.SetCurrentLocation("Online")
Goto RemoveLocations

RemoveLocations:
'----------------------------------------------------------------------
'Remove Locations except Offline and Online
'----------------------------------------------------------------------
Set view = db.GetView( "Locations" )
Set doc = view.GetFirstDocument
If doc.HasItem("Name") Then

While Not(doc Is Nothing)
Set nextdoc=view.GetNextDocument(doc)
Forall subject In doc.GetItemValue("Name")

If subject = "Offline" Then
'Msgbox "Offline Preserved"
Call rtitem.AddNewLine(1)
Call rtItem.Appendtext("Offline Location Preserved")
ElseIf subject = "Online" Then
'Msgbox "Online Preserved"
Call rtitem.AddNewLine(1)
Call rtItem.Appendtext("Online Location Preserved")
Else
Call doc.Remove(True)
'Msgbox subject & " Location Removed"
Call rtitem.AddNewLine(1)
Call rtItem.Appendtext(subject & " *** Location Removed ***")
End If

End Forall

Set doc = nextdoc

Wend

BadDoc:
If Err = 4241 Then
'Msgbox "Error"
Call rtitem.AddNewLine(1)
Call rtItem.Appendtext("!!!Errors Occurred!!!")
Call rtitem.AddNewLine(1)
End If
End If

'----------------------------------------------------------------------
'Get Location from notes.ini
'----------------------------------------------------------------------
location = session.GetEnvironmentString("Location", True)
'Msgbox "Notes.ini Location is set to = " & location
Call rtitem.AddNewLine(1)
Call rtItem.Appendtext("Notes.ini Location is set to = " & location)

'----------------------------------------------------------------------
'Section to Create New Connection Document for Online Location
'----------------------------------------------------------------------
'Search for old location connection documents and remove them
Set view = db.GetView("Connections")
Set doc = view.GetFirstDocument
While Not doc Is Nothing
Set nextdoc=view.GetNextDocument(doc)
Forall NetName In doc.GetItemValue("OptionalNetworkAddress")
If NetName = "firsturl.acme.com" Then
Call doc.Remove(True)
'Msgbox NetName & " Removed"
Call rtitem.AddNewLine(1)
Call rtItem.Appendtext(NetName & " Removed")
Elseif NetName = "secondurl.acme.com" Then
Call doc.Remove(True)
'Msgbox NetName & " Removed"
Call rtitem.AddNewLine(1)
Call rtItem.Appendtext(NetName & " Removed")
End If
End Forall
Set doc=nextdoc
Wend

'Search for online location connection documents create if missing
Set view = db.GetView("Connections")
Set doc = view.GetFirstDocument
While Not doc Is Nothing
Set nextdoc=view.GetNextDocument(doc)
Forall NetName In doc.GetItemValue("OptionalNetworkAddress")
If NetName = "newmainurl.acme.com" Then

'Msgbox NetName & " Already Exists"
Call rtitem.AddNewLine(1)
Call rtItem.Appendtext(NetName & " Already Exists")
Goto SecondConnection
End If
End Forall
Set doc=nextdoc
Wend

'Create Connection document
Call workspace.ComposeDocument( "", "names.nsf", "Connection" )
Set uidoc = workspace.Currentdocument

'Changes the settings of the connection document for the Online Location
Call uidoc.FieldSetText("ConnectionType", "Local Area Network")
Call uidoc.FieldSetText("LanPortName", "TCPIP")
Call uidoc.FieldSetText("Destination", "acme/acme")
Call uidoc.FieldSetText("ConnectionLocation", "Online")
Call uidoc.FieldSetText("ConnectionRecordFirst", "0")
Call uidoc.FieldSetText("Source", "*")
Call uidoc.FieldSetText("OptionalNetworkAddress", "newmailurl.acme.com")

Call uidoc.Save
Call uidoc.Close
'Msgbox "Online NewMainURL Connection Document Created"
Call rtitem.AddNewLine(1)
Call rtItem.Appendtext("Online NewMainURL Connection Document Created")

SecondConnection:
'Search for online location connection documents create if missing
Set view = db.GetView("Connections")
Set doc = view.GetFirstDocument
While Not doc Is Nothing
Set nextdoc=view.GetNextDocument(doc)
Forall NetName In doc.GetItemValue("OptionalNetworkAddress")
If NetName = "newsecondaryurl.acme.com" Then

'Msgbox NetName & " Already Exists"
Call rtitem.AddNewLine(1)
Call rtItem.Appendtext(NetName & " Already Exists")
Goto SendMail
End If
End Forall
Set doc=nextdoc
Wend

'Create Connection document
Call workspace.ComposeDocument( "", "names.nsf", "Connection" )
Set uidoc = workspace.Currentdocument

'Changes the settings of the connection document for the Online Location
Call uidoc.FieldSetText("ConnectionType", "Local Area Network")
Call uidoc.FieldSetText("LanPortName", "TCPIP")
Call uidoc.FieldSetText("Destination", "acme2/acme") 'backup server
Call uidoc.FieldSetText("ConnectionLocation", "Online")
Call uidoc.FieldSetText("ConnectionRecordFirst", "0")
Call uidoc.FieldSetText("Source", "*")
Call uidoc.FieldSetText("OptionalNetworkAddress", "newsecondaryurl.acme.com")

Call uidoc.Save
Call uidoc.Close
'Msgbox "Online NewSecondaryURL Connection Document Created"
Call rtitem.AddNewLine(1)
Call rtItem.Appendtext("Online NewSecondaryURL Connection Document Created")

SendMail:
Call mailDoc.send(False)

Msgbox "Process complete"

End Sub