Create a local replica of your mail file using LotusScript

I recently had the need to create local mail file replicas for a bunch of folks. This is something that *had* to be in place for a certain time but we wanted people to be able to create the replicas at a time of their choosing up until the drop dead date.

So I have two versions of this code. The first, which I’m not posting here because it’s a part of a much larger housekeeping module invoked from the PostOpen event of the user’s mail file, gives no choice to put it off. The end user will receive a dialog indicating what is about to happen and then the replica creation will kick off.

The second, which is sent as a button in an email explaining to the user why they would want to do this and that this allows them to run it at a time of their choosing, I include below. It has some friendlier dialogs for the end user.

When run, the code will pop up a message box explaining what is about to happen and give them an opportunity to stop before they get started.

Then I check to make sure they are the owner of the database (so an admin opening the message on their manager’s behalf doesn’t inadvertently create a local replica of the managers file on their own workstation).

Then – and this is probably overkill – I make sure that the user is explicitly in the ACL (not in there via a group) and is a Manager. In our environment this will always be the case so it doesn’t hurt to leave this check in.  You can probably delete it. This was put in out of a concern that folks would open this in a misconfigured mail-in database (where the owner in the preferences had been allowed to default to the users name instead of that of the mailbox). We manage access to our mail-in databases via groups so it would be unusual for the user to be explicitly listed and HIGHLY unusual for them to have manager access under any circumstances.

I then validate whether or not there already exists a file with the same name and along the same path as the one I wish to create. I’m lifting the path and file directly from the server-side database. I’ve actually come across a few cases where folks have local mail file replicas with completely different names and/or in surprisingly different folders than you would expect. Ah… the creativity of the end user!  For our purposes we need to have the file path and name consistent between the local and server copies.

Note that for the LotusScript Dir$ command, Notes will only return an empty string if the file doesn’t exist. If the containing folder doesn’t exist you will receive an error “76” . But if you create a handler that contains simply “Resume Next” Notes will gleefully pick up INSIDE the conditional statement. I was prepared to create a Boolean variable that would be set if either the file was not there or would be set in the error handler but I was able to take this little bizarre shortcut.

The tricky part for me was to get the new replica to show up automatically on the replication tab. For this I need to thank Notes 411 for a little bit of code that I really should have figured out for myself. This is what the UIWorkspace is doing in the code. Basically you just open both replicas in the UIWorkspace and they will be added to the replication tab. Easy as pie. I did not have any issues getting the replica appear on the workspace as the Notes 411 logic also purports to do.

Once done, I let the user know and exit the script.

This is designed to work on Lotus Notes 6.5.x and 7.

Sorry about the lack of formatting, WordPress seems to have taken to stripping it out.

Sub Click(Source As Button)
‘Check if there is a local mail file replica and create one if it doesn’t already exist.
‘Created by Marc Bourassa – July 16, 2010

Dim Sess As New NotesSession
Dim ws As New NotesUIWorkspace
Dim dbUI As NotesUIDatabase
Dim db As NotesDatabase
Dim ACL As NotesACL
Dim ACLEntry As NotesACLEntry
Dim dbLocal As NotesDatabase
Dim docCalProf As NotesDocument
Dim namName As NotesName
Dim sDataDir As String
Dim iRC As Integer
Dim sMsg As String
Dim sPrintText As String

On Error Goto ErrorGeneral
On Error 4005 Goto ErrorDBExists
On Error 76 Goto ErrorPathNotFound

Set db = sess.Currentdatabase

sMsg = “This button will create a local replica of your mail file (” & db.Title & “) if one does not already exist with the correct name.” & Chr$(13)& Chr$(13)&_
“Please press OK to continue or CANCEL to Abort”

iRC = Messagebox(sMsg,49,”Create Mail File Replica”)

If iRC = 1 Then
Set docCalProf = db.GetProfileDocument(“CalendarProfile”)
Set namName = New NotesName(docCalProf.Owner(0))
If namName.common = sess.Commonusername Then ‘User is the owner of this mail database
Set ACL = db.Acl
Set ACLEntry = ACL.Getentry(namName.Abbreviated )
If Not ACLEntry Is Nothing Then
If ACLEntry.Level=ACLLEVEL_MANAGER Then ‘User is explicitly in ACL AND is a manager
sDataDir = sess.getenvironmentstring(“Directory”,True)
If sDataDir = “” Then ‘something probably wrong with ini file. Don’t expect system to work correctly
Messagebox “There is a problem retrieveing your data directory information from the notes.ini file. Cannot proceed. This issue needs to be resolved by the helpdesk. Aborting Replica Creation.”,0,”Create Mail Database Replica”
Goto LeaveSub
End If
If Dir$( sDataDir & “\” & db.Filepath, 0 ) = “” Then ‘No replica by this name exists. Will fail to next line from error handling if path is bad (i.e. missing mail folder)
Call db.Createreplica(“”, db.Filepath)
‘Now add to the Replication tab
Call ws.opendatabase(db.Server,db.FilePath)
Set dbUI = ws.CurrentDatabase
Call ws.opendatabase(“”,db.FilePath)
Set dbUI=ws.CurrentDatabase
Messagebox “Local mail file replica has been successfully created (” & db.Title & “).”,0,”Create Mail Database Replica”
Else
Messagebox “Replica already exists for your mail file (” & db.Title & “). Nothing to do here.”,0,”Create Mail Database Replica”
End If
End If
Else
Messagebox “You are not a manager of this mail database (” & db.Title & “). Aborting Replica Creation.”,0,”Create Mail Database Replica”
End If
Else
Messagebox “You are not the owner of this mail database (” & db.Title & “). Aborting Replica Creation.”,0,”Create Mail Database Replica”
End If
Else
Messagebox “Replica Creation cancelled by you.”,0,”Create Mail Database Replica”
End If

LeaveSub:
Exit Sub

ErrorDBExists:
Resume LeaveSub

ErrorPathNotFound:
Resume Next

ErrorGeneral:
sPrintText = “Error: ” + Cstr(Err) + ” defn: ” + Error$ + “. ”
Messagebox sPrintText & Chr$(13) & Chr$(13) & “Could not complete this task you will need to create this replica manually. Please contact the help desk for assistance.” ,0,”Unexpected Error”
Resume LeaveSub

End Sub

One thought on “Create a local replica of your mail file using LotusScript”

  1. Hey Marc,
    Thanks for the very good article about creating local replica copy of Mail. Actually I searching the same for one of the my notes application.

Leave a Reply

Your email address will not be published. Required fields are marked *