About This Code
Brief Description:
Locking documents to prevent save conflicts
Contributor:
Andrew Jones
Type:
Document Management
Last Modified:
17 Jun 2002
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
This piece of code assigns edit lock to the first user who opens the document. When other users try to open the same document, they get a message saying "Document is locked by X" and a read-only copy is opened for them. They will not be able to edit the document until the lock is released by the first user.
A manually run agent to unlock the document can be provided for Domino Admin in case of system crash.
Code
Form should have a hidden field called "Lock" of type Text (Editable)
Usage / Example
Sub Queryopen(Source As Notesuidocument, Mode As Integer, Isnewdoc As Variant, Continue As Variant)
If (Source.IsNewDoc) Then
Exit Sub
End If 'New Doc, Lock not reqd
Dim varLock As Variant
Dim session As New NotesSession
Dim nnUserName As New NotesName(session.UserName)
Set docBackend=Source.document
varLock=docBackend.GetItemValue("Lock")
If (varLock(0) <> "") Then
Msgbox ("Document is currently locked by "+varLock(0)+". A read-only copy will be opened for your use.")
Exit Sub
End If
docBackend.Lock=nnUserName.Common
Call docBackend.Save(True,True)
End Sub
'When user trys to edit, check if he/she has the lock
Sub Querymodechange(Source As Notesuidocument, Continue As Variant)
If Source.FieldGetText("Lock")<>"" Then
Dim session As New NotesSession
Dim nnUserName As New NotesName(session.UserName)
If (Strcomp(nnUserName.Common,Source.FieldGetText("Lock"),5)=0) Then
continue=True
Else
Msgbox("Sorry, you are in read-only mode ! This operation cannot be performed. This document is currently used by "+Source.FieldGetText("Lock"))
continue=False
End If
End If
End Sub
'Release the lock when the user with the lock closes the document
Sub Queryclose(Source As Notesuidocument, Continue As Variant)
If strNewDoc="New" Then '(exit if it's a new doc.Set this variable to New in postopen if it's a new doc)
Exit Sub
End If
Dim session As New NotesSession
Dim nnUserName As New NotesName(session.UserName)
If (Strcomp(nnUserName.Common,Source.FieldGetText("Lock"),5)=0) Then
Set docBackend=Source.document
docBackend.Lock=""
Call docBackend.Save(True, True)
End If
End Sub
Agent to Unlock selected documents -
Manually run agent - Simple Action
Modify field 'Lock': Set to ''
Comments
Posted by Walther Barnett on 03/10/2005 02:15:50 AMAvoid error messages in R6 by not using "extended class"
By replacing the queryopen and queryclose with the following
Sub Queryopen(Source As Notesuidocument, Mode As Integer, Isnewdoc As Variant, Continue As Variant)
If (Source.IsNewDoc) Then
Exit Sub
End If 'New Doc, Lock not reqd
Dim varLock As Variant
Dim session As New NotesSession
Dim nnUserName As New NotesName(session.UserName)
Dim item As Notesitem
Set docBackend=Source.document
varLock=docBackend.GetItemValue("Lock")
If (varLock(0) <> "") Then
Msgbox ("Document is currently locked by "+varLock(0)+". A read-only copy will be opened for your use.")
Exit Sub
End If
Set item = docBackend.ReplaceItemValue( "Lock", nnUserName.Common )
Call docBackend.Save(True,True)
End Sub
Sub Queryclose(Source As Notesuidocument, Continue As Variant)
If strNewDoc="New" Then '(exit if it's a new doc.Set this variable to New in postopen if it's a new doc)
Exit Sub
End If
Dim session As New NotesSession
Dim nnUserName As New NotesName(session.UserName)
Dim item As Notesitem
If (Strcomp(nnUserName.Common,Source.FieldGetText("Lock"),5)=0) Then
Set docBackend=Source.document
Set item = docBackend.ReplaceItemValue( "Lock", "" )
Call docBackend.Save(True, True)
End If
End Sub
Posted by Frédéric Gorsse on 11/25/2005 09:06:10 AMPb with attachment file
Dear Andrew,
Is really excellent ! But I have just one pb. I use your code in a forum database. On edited document if you modify an attachment like excel files and an other user try to open the same document he have the message "Document is currently locked by ..." but when the first user after modified attachment saved his doc and close the doc we have a "replication conflict". So strange because original doc and Conflict are the same. Do you know where is the pb ? Could I add a check to not have the conflict ?
Thanks for your code and your help.
Fr■d■ric Gorsse
Posted by Thomas Hug on 09/07/2007 07:37:18 AMNot Notes 6 compatible
This code uses "Lock" which is a reserved word in Notes 6 and later. It won't work there. Besides this, the code in the QueryOpen is on the wrong place, since it does not mean that the user tries to open it. QueryModechange is sufficient to check whether the user tries to edit the document.