Getting the list of users belonging to a group

There is not a method in lotusccript to get the list of all users belonging to a group, and to complicate matters, groups can contain other groups as members.
To solve this problem I wrote getUsersByGroup function that returns the list of users belonging to a group, provided that:

  • the public names.nsf is accessible
  • users have a document “Person” in the public address book
  • groups are defined in the names
  • the maximum number of nested groups is 100, but you can change this setting changing the variable maxNestedGroup


I think that the function getUsersByGroup is also an example of a recursive function that uses the method Static to avoid to define the same variables more times .
The variable incrementIndex defines the increase in the index of the matrix usersTempList when you reach the limit.
The getUsersByGroup function returns a variant containing a string array whose elements are the users in the format [Firstname Lastname] / [domain] / [Country]

Option Public
Option Declare

Sub Initialize

	ReDim usersTempList(10) As String	
	Dim users As Variant
	Dim group As String
	
	group = "[group name]"
	users = getUsersByGroup(group, usersTempList)
	
	ForAll v In users
		Print CStr(v)
	End ForAll
	
End Sub

Sub Terminate
	
End Sub

Function getUsersByGroup(groupName As String, usersTempList() As String) As Variant
	
	Static incrementIndex As Integer
	Static maxNestedGroup As Integer
	Static s As NotesSession
	Static db As NotesDatabase
	Static dbNames As NotesDatabase 
	Static vwGroup As NotesView
	Static vwPerson As NotesView
	Static counter As Integer
	Static nestedCounter As Integer
	Dim doc As NotesDocument
	Dim members As Variant
	Dim user As NotesName
	Dim userName As String
	
	If (s Is Nothing) Then
		Set s = New NotesSession
		Set db = s.Currentdatabase
		Set dbNames = s.Getdatabase(db.server, "names.nsf")
		Set vwGroup = dbNames.Getview("($VIMGroups)")
		vwGroup.Autoupdate = False
		Set vwPerson = dbNames.Getview("($VIMPeople)")
		vwPerson.Autoupdate = False
		counter = 0	
		nestedCounter = 0
		incrementIndex = 10
		maxNestedGroup = 100
	End If
	
	nestedCounter = nestedCounter + 1
	
	Set doc = vwPerson.Getdocumentbykey(groupName, True)

	If Not(doc Is Nothing) Then
		If (counter>UBound(usersTempList)) Then
			ReDim Preserve usersTempList(UBound(usersTempList)+incrementIndex)
		End If
		Set user = New NotesName(doc.FullName(0))
		userName = user.Abbreviated
		usersTempList(counter) = userName
		counter = counter + 1
	Else
		Set doc = vwGroup.Getdocumentbykey(groupName, True)
		If Not(doc Is Nothing And nestedCounter<maxNestedGroup) Then
			members = doc.Getitemvalue("Members")
			ForAll v In members
				Set user = New NotesName(CStr(v))
				userName = user.Abbreviated
				Call getUsersByGroup(userName, usersTempList)
			End ForAll
		End If
	End If
	
	If (nestedCounter=1 And UBound(usersTempList)>1) Then
		getUsersByGroup = FullTrim(usersTempList)
	End If
	nestedCounter= nestedCounter - 1

End Function

Comments

2 responses to “Getting the list of users belonging to a group”

  1. Thank you very much, very useful.

  2. Very useful.
    Thank you

Leave a Reply to maros Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.