Create Active Directory Contact and then add them to a Distribution List

kkonkle

New member
Joined
Mar 12, 2012
Messages
2
Programming Experience
10+
I searched this site, as well as Google, but haven't been able to find what I need.

I'm creating a VB.Net program that will create users in our Active Directory and add them to a mailing list we have created.

I have the code for creating the contact down, and that works great. I'll link it below for reference.

What I need to do now is add that new contact to the distribution list, and I am not sure how to accomplish that.

VB.NET:
[SIZE=2]Dim oRootSam [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] DirectoryEntry [/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]=[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] New DirectoryEntry[/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]([/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][URL]ldap://ou=SAMMONS REPRESENTATIVES,ou=Email Distribution,DC=sigmafin,DC=sigmafinancial,DC=com[/URL][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080])
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]
Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] newContact [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] DirectoryEntry[/COLOR]
newContact = oRootSam.Children.Add(cnName, [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Contact"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])

[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Set properties[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]
newContact.Properties([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"GivenName"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Add(fName)
newContact.Properties([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"sn"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Add(lName)
newContact.Properties([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"DisplayName"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Add(fName & [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & lName)
newContact.Properties([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"mail"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Add(email)
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]
'Save the new contact[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]
newContact.CommitChanges()
[/SIZE][/SIZE][/COLOR][/SIZE]

At this point I can open Active Directory and find the new contact.

Does anyone know how to then take this contact and add them to a distribution list? I need to open the Contact and go to the "Member of" tab and see the correct list displayed there.
 
OK, after a lot more Googling around I found a way to do it. Not too hard, but you have to know to use the Invoke() function.

I'll post it here in case someone else needs to know how to do this in the future:

VB.NET:
[SIZE=2][COLOR=#008000]'This code goes right below the [SIZE=2]newContact.CommitChanges() line from above[/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim [/COLOR]groupSam [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] DirectoryEntry [/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]=[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] New DirectoryEntry[/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]([/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][URL]ldap://ou=SAMMONS REPRESENTATIVES DISTRIBUTION LIST,ou=lists,ou=Email Distribution,DC=S...nancial,DC=com[/URL][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080])
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] oRootGroup [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] DirectoryEntry [SIZE=2]= new DirectoryEntry(groupSam)[/SIZE]

oRootGroup.Invoke("[COLOR=#a31515]Add[/COLOR]", New Object() {newContact.Path.ToString})

[SIZE=2][COLOR=#008000]'Save the updated group[/COLOR][/SIZE]
oRootGroup.CommitChanges()
[/COLOR][/SIZE][/COLOR][/SIZE]
 
Back
Top