Tuesday, 14 November 2017

Update Readers and Authors Fields Using LotusScript - Adding New Values

During the past months I have worked a lot with LotusScript. One of the questions I came accross was how to add a value to a Readers or Authors field in the UI document using LotusScript. First a little background information. In daily practice, I make extensive use of Userroles in the Access Control List which are assigned to specific User Groups in the Access Control List. Based on these Userroles document access is granted based on these Userroles in the Authors and Readers Fields in the specific document.
Basic idea is to grant Edit or Read Access to specific User Groups depending on where the document is located within a certain workflow. More on that in one of the next blog posts.
In this specific case there was a value missing whereby certain groups of users were unable to read the documents.
In the example below my solution, in which the Readers Field including the new Value is stored back to the UI document using LotusScript.
Note: In the example below I get the values (uidoc.Document.GetItemValue) of two fields, Username and OrganizationalUnit, from the selected UI document in a View and add these values to a Readers Field.
The LotusScript Agent with the following code I use on selected documents in a Notes view to update the Readers (or Authors) fields.

Sub Initialize
Dim ws As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim fieldValue As Variant
Dim fieldValue2 As Variant

' check if document is selected
If Not ws.CurrentDocument Is Nothing Then

' document is selected
Set uidoc = ws.CurrentDocument' init
End If

fieldValue = uidoc.Document.GetItemValue("FIELDNAME1")
fieldValue2 = uidoc.Document.GetItemValue("FIELDNAME2")
Dim item As NotesItem
Dim readersList(4) As String
readersList(0) = "[USERROLE]"
readersList(1) = fieldValue(0) '@UserName
readersList(2) = "[USERROLE]"
readersList(3) = "[" & fieldValue2(0) & "Reader]" 'OrganizationalUnit Reader(s)
readersList(4) = "[USERROLE]"
readersList(4) = "[NEWUSERROLE]" ' New Value

If uidoc.Document.HasItem("Readers") Then
Set item = uidoc.Document.GetFirstItem("Readers")
Call item.Remove
End If
Set item = New NotesItem(uidoc.Document, "Readers", readersList, READERS)

' save document
Call uidoc.Document.Save(True, True)

End Sub

A quick and easy solution using LotusScript.

No comments:

Post a Comment