Finding emails for users in an AD Group

  • 30 November 2006
  • 2 replies
  • 1 view

Badge +13
Here is the code that emails each user in the AD group.

Function SMTPGroupTemplate(ByVal K2 As ClientEventContext, ByVal strADGroup As String)
'To be called from Client Event
'Purpose: Emails each member in the AD group

Dim group_entry As New System.DirectoryServices.DirectoryEntry("LDAP://CN=" & strADGroup & ",OU=yourcompany User Groups,DC=yourcompany,DC=Com")
Dim strGroup_member As String
Dim entry As New System.DirectoryServices.DirectoryEntry
Dim searcher As New System.DirectoryServices.DirectorySearcher
Dim result As System.DirectoryServices.SearchResult
Dim sEmail As String = String.empty

For Each strGroup_member In group_entry.Properties("member")
entry = New System.DirectoryServices.DirectoryEntry("LDAP://" & strGroup_member)

searcher = New System.DirectoryServices.DirectorySearcher(entry)
searcher.PropertiesToLoad.Add("mail")

result = searcher.FindOne
If Not result Is Nothing Then
Try
sEmail = result.Properties("mail")(0).ToString()
SMTPFunction(K2, sEmail)
Catch ex As exception
'suppress error in case AD user does not have email address
End Try
End If
Next
End Function

2 replies

Badge +10
Hi,

Technically it will be possible to get the members of the group by using the DSHelper, but because this is used internally in K2.net it is not supported and also not recommended that you use it.

Using .Net functions to return the list of users will work.
To get the code to work I would suggest that you look through MSDN on the exact way to get the members of a group.

Regards,

JohanL
Badge +5
Hi,
Just for some extra info...
Peter's code would work with non-nested groups, but as soon as you might have some nested groups you would just have to modify the code to load also the objectClass property and test if the object is a 'group' or 'person'. Obviously recursion would have to come in play when working with nested groups.

Cheers

Reply