Click here to Skip to main content
11,636,889 members (75,014 online)
Click here to Skip to main content

Tagged as

Set/Update Active Directory attributes to user

, 24 Jan 2013 CPOL 13K 11
Rate this:
Please Sign up or sign in to vote.
How to set/update Active Directory attributes to user.


I was looking for code to easily update the accounts information in Active Directory. I got inspired from a very good article "Howto: (Almost) Everything In Active Directory via C#" which adds everything except this little part I am adding now. I am getting information from a MS-SQL database and run one account after the other to update the info.

This is just an extract of the code...

Using the code

A call to this class might look like:

myObjectReference.SetAdInfo("sAMAccountName" , Property.Title, "Vice President", "")

The same calls can be done by setting the property to one of the items enumerated in the "public enum Property" object in this class:

//Example of call: 	ADWork ADStuff = new ADWork();
//	ADStuff.SetAdInfo(newemployeeID, ADWork.Property.title, newtitle, "");
using System;
using System.Collections;
using System.Text;
using System.IO;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices;
using System.Data;
using System.Configuration;

class ADWork
    public void SetAdInfo(string objectFilter, Property objectName, 
                string objectValue, string LdapDomain)
        string connectionPrefix = "LDAP://" + LdapDomain;
        DirectoryEntry entry = new DirectoryEntry(connectionPrefix);
        DirectorySearcher mySearcher = new DirectorySearcher(entry);
        mySearcher.Filter = "(cn=" + objectFilter + ")";
        SearchResult result = mySearcher.FindOne();
        if (result != null)
            DirectoryEntry entryToUpdate = result.GetDirectoryEntry();
            if (!(String.IsNullOrEmpty(objectValue)))
                if (result.Properties.Contains("" + objectName + ""))
                    entryToUpdate.Properties["" + objectName + ""].Value = objectValue;
                    entryToUpdate.Properties["" + objectName + ""].Add(objectValue);

	public enum Property

Hope it will be useful to everyone...


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

France France
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150728.1 | Last Updated 24 Jan 2013
Article Copyright 2012 by cberthu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid