Last post Jul 31, 2008 06:04 AM by EveCalypso
Jul 21, 2008 06:35 AM|EveCalypso|LINK
I can not seem to find much on the Net on how to add/remove users from a given distribution list programmatically.
Can anyone point me in the right direction please? I am net to ASP.NET and Directory Services! I can update a users' properties and list all users in a distribution list so far, but my attempted code at the above is not working. I am obviously missing
something fundamental in my understanding of the class.
Jul 21, 2008 07:23 AM|farazsk11|LINK
have a look at this it may help you out
Jul 21, 2008 07:34 AM|EveCalypso|LINK
Hello, yes I have seen this link before. I can actually list users in a distribution list, I just can not seem to Add or Remove them.
Thank you for reply.
Jul 21, 2008 08:15 AM|farazsk11|LINK
You can also use DirectoryEntry's Invoke method like de.Invoice("Remove",.....)
In one of my applications I had to put this code see if you can modify it according to your requirement
LdapTree = Root.Children.Find(
For me ActiveDirectories are always confusing :)
Jul 21, 2008 09:04 AM|EveCalypso|LINK
Directories are okay - it's the .NET Directory class that confuses me :)
I am sure there has to be an elegant way to say:
here is the group, here is the user, if the user is not already in the group, please add user.
here is the group, here is the user to remove, if the user is in the group, please delete, else do nothing.
I wish it was as simple as that [:)]
Jul 31, 2008 06:04 AM|EveCalypso|LINK
I managed to get this working myself - probably not very pretty, but it does the trick. If you see a major problem, please let me know - a newbie afterall
PS: This is part of a class I am working on
'Adding a User to a given Distribution List
Public Function AddUserToADistributionList(ByVal strDListName As String, Optional ByVal strUserName As String = "") As String
AddUserToADistributionList = ""
If Len(Trim(strUserName)) = 0 Then
If Len(Trim(setLogin)) <> 0 Then
strUserName = setLogin
AddUserToADistributionList = "No User Specified, Please use 'setLogin' or supply a user explicitly."
If Len(Trim(strDListName)) = 0 Then
AddUserToADistributionList = "No Distribution List Specified, Please supply a Distribution List."
If Not ListExists(strDListName) Then
' function throws exception if empty list
AddUserToADistributionList = "List does not exist"
Dim dom As New DirectoryServices.DirectoryEntry
dom = GetDirectoryEntry() ' This is a function which sets server, passwd etc and returns a DirectoryEntry object
Dim usr1 As New DirectoryServices.DirectorySearcher(dom)
Dim grp1 As New DirectoryServices.DirectorySearcher(dom)
usr1.Filter = "sAMAccountName=" & strUserName
Dim usr1Result As DirectoryServices.SearchResult = usr1.FindOne
Dim allGroups As String
Dim bInGroup As Boolean
grp1.Filter = "CN=" & Trim(strDListName)
Dim grp1Result As DirectoryServices.SearchResult = grp1.FindOne
Dim grp As New DirectoryServices.DirectoryEntry
For Each allGroups In usr1Result.GetDirectoryEntry.Properties("memberof").Value
If allGroups = grp1Result.GetDirectoryEntry.Properties("distinguishedName").Value Then
bInGroup = True
AddUserToADistributionList = "You are already in the " & strDListName & " Distribution List"
bInGroup = False
If Not (bInGroup) Then
grp = grp1Result.GetDirectoryEntry
Dim usr As DirectoryServices.DirectoryEntry
usr = usr1Result.GetDirectoryEntry
AddUserToADistributionList = "Adding " & strUserName & " to " & strDListName & " was successful."
usr1Result = Nothing
grp1Result = Nothing
Catch ex As Exception
AddUserToADistributionList = "Adding " & strUserName & " to " & strDListName & " was unsuccessful. An error occured."