Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

HOW TO: Find Distribution List Membership for a Given Recipient


View products that this article applies to.

Summary

You can use the Active Directory Service Interfaces (ADSI) to obtain the distribution list membership for a given Microsoft Exchange Server Recipient object. This step-by-step article describes how to accomplish this using either Microsoft Visual C++ or Microsoft Visual Basic.

NOTE: This is an alternative to using the members collection returned by the IADsUser::Groups method. One key difference is that enumerating the members collection will give you the group objects rather than the group names. See the documentation for IADsUser::Groups for more information.

Find a Distribution List Membership

An Exchange Server Recipient object holds its distribution list(s) membership through the LDAP memberOf attribute. This attribute is either returned as a single string or a variant array of strings.

The following code will bind to a Recipient object using LDAP, cache the membership attribute, and print the distribution list(s) to the screen.

Using Visual C++

/*
   include activeds.h
   link with activeds.lib and adsiid.lib
*/ 

HRESULT func ()
{
   HRESULT hr;
   IADs *pADs;
   VARIANT pvProp;
   BSTR bstrpropName;
   long lLBound, lUBound;
      
   // Initialization
   VariantInit(&pvProp);
   CoInitialize(NULL);
   bstrpropName = SysAllocString(L"memberOf");
      
   // Bind to an Exchange recipient object
   hr = ADsGetObject(L"LDAP://servername/o=org/ou=site/cn=recipients/cn=fhc", IID_IADs, (void**) &pADs );
   if ( !SUCCEEDED(hr) )
   {
      return hr;
   }
      
   // Get recipient's members
   hr = pADs->Get(bstrpropName, &pvProp);
   if (FAILED(hr))
   {
      return hr;
   }
   
   if (VT_BSTR == pvProp.vt)  /* only one DL */ 
   {
      printf ("%S\n", pvProp.bstrVal);
   }
   else                 /* multiple DLs in an array */ 
   {
      SafeArrayGetLBound(pvProp.parray, 1, &lLBound);
      SafeArrayGetUBound(pvProp.parray, 1, &lUBound);
      
      for (long i = lLBound; i <= lUBound; i++)
      {
         VARIANT svar;
         SafeArrayGetElement(pvProp.parray, &i, &svar);
         if (VT_BSTR == svar.vt && svar.bstrVal)
            printf("\n%S",svar.bstrVal);
      }
   }
   
   // Releasing
   VariantClear(&pvProp);
   SysFreeString (bstrpropName);
   pADs->Release();
   
   return S_OK;
}
				

Using Visual Basic


Dim RecipObj As IADs
Dim memberOf As Variant

Set RecipObj = GetObject("LDAP://servername/o=org/ou=site/cn=recipients/cn=fhc")
memberOf = RecipObj.Get("memberOf")

If IsArray(memberOf) Then 'User is on more than one DL
    For Each member In memberOf
       Debug.Print member
    Next
Else
    Debug.Print memberOf
End If
				

↑ Back to the top


References

For more information about Active Directory Services Interfaces (ADSI), please refer to the following Web site:

↑ Back to the top


Properties

Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

↑ Back to the top


Keywords: KB220043, kbmsg, kbhowtomaster, kbfaq

↑ Back to the top

Article Info
Article ID : 220043
Revision : 5
Created on : 9/28/2007
Published on : 9/28/2007
Exists online : False
Views : 331