Geeks With Blogs
Naresh S A Beginner's Blog
| Home |

This article explains how to add the users to a PDL programmatically.


In large organizations, most of the employees may belong to more than one project project/work groups. Each project/workgroup maintains a separate distribution list for communicating with its members. As number of members in a workgroup increases, maintaining the PDL becomes an overhead. One way to do it is to automate the process


In this process, official email-id of the all members are entered in a text file. We will read from the text file and add the members to the PDL. This is done using C# and Active Directory with LDAP


Libraries Required:

  • ActiveDS  :  Add References à Com à Active DS Type Library
  • System.DirectoryServices : Add References à  .NET à System.DirectoryServices.dll

In the text file(in our example : C:\namelist.txt), enter the email ids a new line character

using System;
using ActiveDs;
using System.DirectoryServices;
using System.IO;
using System.Collections;

namespace PDL
 class pdl
  static void Main(string[] args)
   FileStream file;
   StreamReader reader;
   ArrayList namesList;
   string line;
   string pdlname = "Your PDL name";
   string filename = "C:\namelist.txt";
   DirectoryEntry activeDir = new DirectoryEntry(“LDAP://=“+pdlname+“,OU=Distribution Lists
,DC=net“); // Give proper LDAP path of the pdl
   Console.WriteLine("\n\n Starting....");
    //Read names from a file and add them to the DL
    file        = new FileStream(@filename,FileMode.OpenOrCreate,FileAccess.ReadWrite);
    reader      = new StreamReader(file);
    namesList   = new ArrayList();
    while((line = reader.ReadLine())!= null)
     namesList.Add("CN="+line+",OU=Users,DC=net");// Give proper LDAP path of the pdl
    for(int i=0; i<namesList.Count; i++)
if(DirectoryEntry.Exists("LDAP://"+namesList[i].ToString())) // Check whether valid employee
      { //Check if the member already exists in the PDL
      catch(Exception ex)
       Console.WriteLine("Error: "+ex.Message + " :  " +namesList[i].ToString());
      Console.WriteLine(namesList[i].ToString() + "is invalid");
    activeDir.RefreshCache(new string[]{"member"});
    Console.WriteLine("\n\n Completed.");
   catch(Exception e)
    Console.WriteLine("Exception Occured : " +e.Message);

Posted on Thursday, July 13, 2006 10:04 AM | Back to top

Comments on this post: Adding a user to a Public Distribution List in a Active Directory

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Naresh S | Powered by: