开发者

Updating properties for multiple users

How do I update a list of different Telephone, IPPhone using this

    static void Main(string[] args)
    {
        Console.Write("Enter userid      : "); // I would pass this in from the first 
                                               //Field in the .csv file 2439009
        String username = Console.ReadLine();

        try
        {
            DirectoryEntry myLdapConnection = createDirectoryEntry();

            DirectorySear开发者_开发百科cher search = new DirectorySearcher(myLdapConnection);
            search.Filter = "(cn=" + uid + ")";
            search.PropertiesToLoad.Add("Telephone","IPPhone");

            SearchResult result = search.FindOne();

            if (result != null)
            {
                // create new object from search result

                DirectoryEntry entryToUpdate = result.GetDirectoryEntry();

                // show existing title

                Console.WriteLine("Current title   : " + entryToUpdate.Properties["Telephone][0].ToString());
                Console.Write("\n\nEnter new title : ");

                // get new title and write to AD

                String newTitle = Console.ReadLine();

                entryToUpdate.Properties["Telephone"].Value = newTelePhone;
                entryToUpdate.Properties["IPPhone"].Value = newIPPhone;

                entryToUpdate.CommitChanges();

                Console.WriteLine("\n\n...new title saved");
            }

            else Console.WriteLine("User not found!");
        }

        catch (Exception e)
        {
            Console.WriteLine("Exception caught:\n\n" + e.ToString());
        }
    }

    static DirectoryEntry createDirectoryEntry()
    {
        // create and return new LDAP connection with desired settings

        DirectoryEntry ldapConnection = new DirectoryEntry("mydomain.dm.com");
        ldapConnection.Path = "LDAP://OU=myusers,DC=sales,DC=US,DC=US";
        ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
        return ldapConnection;
    }


I'm guessing you've grabbed someone else's code and don't know how to use it?

You should understand that this code can (will?) cause serious server problems as the DirectoryEntry resources are not closed correctly.

Every DirectoryEntry variable in your Main method should be wrapped in a using(){} statement.


Try something like this:

You define a class CSVRecord which holds your data from the CSV - read that in using FileHelpers. The class looks like this:

public class CSVRecord
{
    public string EmployeeNumber { get; set; }
    public string TelephoneNumber { get; set; }
    public string IPPhoneNumber { get; set; }
}

Once you've read that class in, you need to iterate over its elements, and do the update for each of them.

CSVRecord[] listOfEmployees = (read in via FileHelpers)

// define root for searching your user accounts    
using (DirectoryEntry root = new DirectoryEntry("LDAP://dc=yourcompany,dc=com"))
{
    // set up directory searcher to find users by employeeId
    using (DirectorySearcher searcher = new DirectorySearcher(root))
    {
       searcher.SearchScope = SearchScope.Subtree;

       // iterate over all entries in your list of employees     
       foreach (CSVRecord csvEntry in listOfEmployees)
       {
           searcher.Filter = string.Format("(&(objectCategory=user)(employeeId={0}))", csvEntry.EmployeeNumber);

           // search for that employee         
           SearchResult result = searcher.FindOne();

           // if found - access the DirectoryEntry      
           if (result != null)
           {
               DirectoryEntry foundUser = result.GetDirectoryEntry();

               // update properties from values in CSV
               foundUser.Properties["telephoneNumber"].Value = csvEntry.TelephoneNumber;
               foundUser.Properties["ipPhone"].Value = csvEntry.IPPhoneNumber;

               // save changes back to directory
               foundUser.CommitChanges();
            }
        }
    }
}

Does that work for you??

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜