Partager via


Visual Basic Code Example: Creating an Active Directory Domain Services Distribution List

 

Applies To: Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server Technical Preview, Windows Vista

This example provides a Private Sub procedure that creates an Active Directory Domain Services (AD DS) distribution list with the name supplied by the caller in the local domain.

For information on how distribution lists are used, see Distribution Lists.

For an example of how to send messages to distribution lists, see Visual Basic Code Example: Sending Messages Using Distribution Lists.

This example uses the GetObject method to obtain an ADSI object that represents the root of the directory data tree on a directory server in the local domain (rootDSE). RootDSE is a unique entry that exists on every directory server. It enables you to obtain information about the server. In this example, it is used to obtain the distinguished name (DN) of the first domain (the root domain) in the forest that contains the domain to which the directory server belongs. This name is then bound to an ADSI container object, in which the distribution group object is created.

Note

Distribution Lists are AD DS objects that can be created only in the Computers and Domain Controllers containers of domain controllers. This example can be run only on a domain controller by a user having domain administrative permissions.

To run this procedure, you must include the Active DS Type Library as a resource in your Visual Basic® project.

To create a distribution list

  1. Declare the variables and objects needed.

  2. Call GetObject to obtain an ADSI object for rootDSE.

  3. Call IADs.Get to retrieve the domain name from the ADSI object representing rootDSE.

  4. Bind the domain name to an ADSI container object that will serve as the parent ADSI container for the distribution list.

  5. Call IADsContainer.Create to create a distribution list (DL) with the name supplied by the caller. The distribution list is created in the form of an AD DS distribution group in the parent ADSI container.

  6. Set the group type of the distribution list to global distribution group.

Code Example

The following code example requires MSMQ 3.0.

Private Sub CreateDL( _  
                     strDLName As String _  
                     )  
  
  Dim iadsRootDse As IADs  
  Dim strRootDomain As String  
  Dim iadscontDS As IADsContainer  
  Dim iadsgroupDist As IADsGroup  
  Dim iGroupType As Integer  
  
  ' Get an ADSI object for rootDSE.  
  On Error GoTo ErrorHandler  
  Set iadsRootDse = GetObject("LDAP://rootDSE")  
  
  ' Retrieve the domain name from the object representing rootDSE.  
  strRootDomain = iadsRootDse.Get("RootDomainNamingContext")  
  
  ' Bind the domain name to the parent ADSI container.  
  Set iadscontDS = GetObject("LDAP://" + strRootDomain)  
  
  ' Create a distribution list (DL) in the form of an AD DS  
  ' distribution group in the parent ADSI container.  
  Set iadsgroupDist = iadscontDS.Create("group", "CN=" + strDLName)  
  
  'Set the group type of the distribution list to global distribution group.  
  iGroupType = ADS_GROUP_TYPE_GLOBAL_GROUP  
  iadsgroupDist.Put "sAMAccountName", strDLName  
  iadsgroupDist.Put "groupType", iGroupType  
  iadsgroupDist.SetInfo  
  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Error " + Hex(Err.Number) + " returned." _  
          + Chr(13) + Err.Description  
  
End Sub