Posted on:
Categories: FIM
Description:

Issue:

Searching multi-domain single forest environment for an attribute through “managed code” has been an issue for me. Searching using Powershell is relatively easy but I needed to integrate managed code into an existing application and below is what I came up with. The code is easy to understand and follow. Any questions feel free to drop me an email mjaffer[@]softlanding.ca

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Collections;
using System.Diagnostics;
namespace ConsoleSearcher
{
    class Program
    {
        static void Main(string[] args)
        {
            var allDomains = Forest.GetCurrentForest().Domains.Cast<Domain>();
            int x=0;
           
            foreach (Domain d in allDomains)
            {
                //Console.WriteLine("Arguement: " + args[0]);
                var searcher = new DirectorySearcher(new DirectoryEntry("LDAP://" + d.Name));
                searcher.Filter = "(&(objectClass=user)(employeeid="+args[0]+"))";
                //SearchResultCollection results = searcher.FindAll();
                SearchResult result = searcher.FindOne();
                ResultPropertyCollection myResultPropColl;
               
                if (result != null)
                {
                    myResultPropColl = result.Properties;
                    x++;
                    Console.WriteLine("Found it");
                    // Console.WriteLine(result.Properties["Displayname"]);
                   
                    Console.WriteLine("The properties of the " +
                                      "'mySearchResult' are :");
                    foreach (string myKey in myResultPropColl.PropertyNames)
                    {
                        string tab = "    ";
                        Console.WriteLine(myKey + " = ");
                        foreach (Object myCollection in myResultPropColl[myKey])
                        {
                            Console.WriteLine(tab + myCollection);
                        }
                    }
 
                }
            }
            if (x == 0)
            {
                Console.WriteLine("Didnt find it");
            }
            }
        }
    }