Document locking with domino6

Hi,I have a shared DB (25 users +) and am trying to implement the new LN6 document locking. This DB is also replicated on 1 other server.

What Im trying to do is:

  1. I want to prevent users to reply to a email which another user is already busy replying to. (With a message box indicating who is currently replying to the email).

What I have tried:

  • I enabled document locking on the DB.

  • Created the $Writers field on the form.

  • I put the following code in the postopen event of the reply with history form:

Sub Postopen(Source As Notesuidocument)

Dim docBackend As NotesDocument

Set docBackend=Source.document



varLock=docBackend.GetItemValue("$Writers")

If (	varLock(0) <> "" ) Then

	Msgbox ("Document is currently locked by "+varLock(0)+"")

	Call Source.close

	Exit Sub

End If





Call cMemoObject.PostOpen(Source)	

End Sub

This works to the extent that it shows if a document is locked, by who, and exits. However I dont know how to actually lock the document (if its not locked) and then how to unlock the document after it has been replied to /sent again. I know I can do it manually from Actions-> Lock/Unlock Doc, but I want this to be done automatically when someone hits reply with history.

Any help would be helpfull, or other ways of going about this.

Thanks,

Fabio

Subject: Document locking with domino6

Hi Fabio,

You don’t actually need to create the $Writers field on the form - locking adds the fields automatically, and the lockHolders property of notesdocument allows you to view who the lockholders are.

Simply add

Call notesdocument.lock to your code. This locks the document using the effective user’s ID. Alternatively you can pass in one or more names for the lock.

You can unlock the document using

Call notesdocument.unlock

Note that database managers can break document locks by default, so you may need to use other functionality to stop this happening.

Here’s a sample of the code I use to lock a document over the web - this agent runs as web user.

Also, note that due to the odd way this has been implemented by Lotus, the lockholders array is empty the first time a document is locked, but after that it will always have a single entry of null value (i.e. “”) - hence the checks in the code.

I use the lockStatus fields, etc to determine who can see which buttons on the form.

Sub Initialize

%REM

The docLockStatus field is used to determine whether someone can edit a document

0 - Document is unlocked and can be edited

1 - Document is locked by another user and cannot be edited

2 - Document is locked by current user and can be edited only by that user.

%ENDREM

Set session = New Notessession

Set db = session.currentDatabase

Set doc = session.documentContext

Set un = New NotesName(session.EffectiveUserName)

’ Check if the document is being edited

If doc.inedit(0) = "1" Then

	editMode = True

Else

	editMode = False

End If



If db.IsDocumentLockingEnabled = True Then

	If Isempty(doc.LockHolders) Or doc.LockHolders(0) = "" Then ' the document can be edited

		doc.docLockStatus = 0

		If editMode = True Then Call doc.Lock ' only lock the document when edited

	Else

		Set locker = New NotesName(doc.LockHolders(0))

		doc.LockedBy = locker.common ' write name of lock holder to doc field

		If doc.LockHolders(0) = un.Canonical Then ' // test if this is the lock holder

			doc.docLockStatus = 2

		Else

			doc.docLockStatus = 1

		End If

	End If

Else

	Msgbox "ERROR - Document Locking NOT enabled in " & db.filepath

End If

End Sub

Subject: RE: Document locking with domino6

Hi Giles,

I tried all of the options but lock or unlock doesn’t seem to work. It would be useful if you let me know step by step process

Subject: RE: Document locking with domino6

Hi, Thanks for the reply!

I tried your code in the (sub postopen) of the RWH form, but I cannot find a way to access the document(that Im replying to) properties. I know I can do this from an agent (using session.document context) but I having been going crazy trying to figure out how to access fields from within the form.

eg: I can get the document properties using

Set session = New Notessession

Set db = session.currentDatabase

Set doc = session.documentContext

from the (sub postopen).

Do you know how I could do this?

I would like to do all the checks from within the form, otherwise the only other way I know is to use "@Command( [ToolsRunMacro] ; agent ) " and call up the RWH form from within the agent.

I really appreciate your help on this.

Thanks,

Fabio

Subject: RE: Document locking with domino6

If you’re doing this client side, then documentContext isn’t the way to access the current document, as it’s a UIDocument object.

To get a handle on the back end document, use

Dim ws As New NotesUIWorkspace

Dim uidoc As NotesUIDocument

Dim doc As NotesDocument

Set uidoc = ws.currentDocument

Set doc = uidoc.document

Here, uidoc is a NotesUIDocument which allows you to access current information about the document on screen.

The doc is a NotesDocument representing what’s held in the back end (which may not have been saved yet).

Subject: RE: Document locking with domino6

Hi,

Im going crazy trying to figure this out. Ok, so:

"The doc is a NotesDocument representing what’s held in the back end "

Am I correct by saying that this is not the doc that is selected when I click on the RWH button?

I tried the following in the sub postopen of the RWH button, this is a very simple version of what I want to do but I cant even get the checking and locking to work.

Sub Postopen(Source As Notesuidocument)

Set session = New Notessession

Set db = session.currentDatabase

Dim doc As NotesDocument

Set doc = Source.document

Set un = New NotesName(session.EffectiveUserName)



If db.IsDocumentLockingEnabled = True Then

	

	If doc.LockHolders(0) = "" Then

		Call doc.Lock

		Call doc.Save(True, True)

	Else

		Messagebox("document already locked")

		

	End If

	

	

Else 

	Messagebox("Document locking not enabled on DB")

End If





Call cMemoObject.PostOpen(Source)	

End Sub

When I click the RWH, I get the error “Invalid or nonexistant document”, this happens when trying to do doc.lock. If I save the doc before “doc.lock” (just inserted call doc.save(True, True) it works and I can see my user name in the doc.lockHolders(0) . However then if I try with another user to reply to the same email using the same RWH button, The doc.lockholders is empty, so it locks it again. (I really dont know what is going on now). I assume this “doc” is the actuall reply that is being created, but I need to set the original doc so nobody can reply to it.

Also, What is the difference if I go on Actions->Lock Document or if I do a doc.Lock from lotus script?

I really appreciate your help with this, I have done a bit of lotus script (like sorters, archiving, Dblookups and setting fields on docs, but not much within forms and I feel like Ill never get this part.

Thanks,

Fabio

Subject: RE: Document locking with domino6

Hi Fabio,

You won’t need to lock the document when you first create it as it won’t have a corresponding back end document until you first save it - that explains the non existant document message you get.

Another point is that you don’t actually need to save the document in order to lock it. Once you call the lock method it will do all this automatically for you.

I’m trying to understand what you need to do, so tell me if I get this right or not.

By the sounds of it, you want to lock an email or document while a user replies to it so noone else can.

You could do this by locking the original document when clicking the reply button. So for example, imagine you’re in the original document and you have a button to open the new reply / response. The code would look something like this:

Sub thisButton(…)

Dim ws As New NotesUIWorkspace

Dim uidb As NotesUIDatabase

Dim db As NotesDatabase

Dim uidoc As NotesUIDocument

Dim doc As NotesDocument

Set uidb = ws.currentDatabase

Set db = uidb.database

Set uidoc = ws.currentDocument

Set doc = uidoc.document

If Not uidoc.IsNewDoc Then ’ check it’s not a new document

If db.isDocumentLockingEnabled = True Then

If IsEmpty(doc.LockHolders) Or doc.lockHolders(0) = “” Then

Call doc.lock ’ // this will lock the original document with the effective username

’ // YOUR CODE TO COMPOSE THE RESPONSE / REPLY GOES HERE…

Else

MsgBox "This document is already being edited by: " & doc.LockHolders(0)

End If

Else

MsgBox “Locking is not enabled. Exiting…”

Exit Sub

End If

End If

End Sub

If this doesn’t work, try

Subject: RE: Document locking with domino6

Hi,

Thanks sooo much for your reply, you made me see the light in some respects.

By the sounds of it, you want to lock an email or document while a user replies to it so noone else can.

  • Thats exactly what Im trying to do.

The only thing is that I also want this to work without opening the doc (double clicking on it) and then click reply, but also just want to be able to select the document in a folder and then click on reply.

I have managed to do that with the following code:

Sub Click(Source As Button)

Dim ws As New NotesUIWorkspace

Dim uidb As NotesUIDatabase

Dim db As NotesDatabase

Dim uidoc As NotesUIDocument

Dim doc As NotesDocument

Set uidb = ws.currentDatabase

Set db = uidb.database





Call ws.EditDocument( False )



Set uidoc = ws.currentDocument

Set doc = uidoc.document



If Not uidoc.IsNewDoc Then ' check it's not a new document

	If db.isDocumentLockingEnabled = True Then

		If Isempty(doc.LockHolders) Or doc.lockHolders(0) = "" Then

			Call uidoc.Close

			Call doc.lock

			Set uidoc = ws.ComposeDocument( "", "", "Reply With History" )     

			Call uidoc.GoToField( "Body" )     

			

		Else

			Msgbox "This document is already being edited by: " & doc.LockHolders(0)

			Call uidoc.Close

		End If

	Else

		Msgbox "Locking is not enabled.  Exiting.."

		Exit Sub

	End If

End If

Now the only problem I have is to unlock it again when the document is sent. I tried adding in “doc.unlock” in the sub terminate of the buuton but it gets ignored and looks like it executes that part when closing the DB. Where would I have to put that part in my code, I know Im calling up the “reply with history” in the above code but when that finishes executing I need to tell it to unlock the doc again.

Thanks for your help!

Fabio

Subject: RE: Document locking with domino6

What action do you perform with docLockStatus = 1?