Click here to Skip to main content
15,881,173 members
Articles / Programming Languages / C#
Tip/Trick

Set/Update Active Directory attributes to user

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
24 Jan 2013CPOL 46.4K   14   1
How to set/update Active Directory attributes to user.

Introduction

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:

C#
myObjectReference.SetAdInfo("sAMAccountName" , Property.Title, "Vice President", "company.org")

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:

C#
//
//Example of call: 	ADWork ADStuff = new ADWork();
//	ADStuff.SetAdInfo(newemployeeID, ADWork.Property.title, newtitle, "CompanyDomain.org");
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 + ")";
        mySearcher.PropertiesToLoad.Add(""+objectName+"");
        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;
                }
                else
                {
                    entryToUpdate.Properties["" + objectName + ""].Add(objectValue);
                }
                entryToUpdate.CommitChanges();
            }
        }
        entry.Close();
        entry.Dispose();
        mySearcher.Dispose();
    }

	public enum Property
    {
         title,displayName,sn,l,postalCode,physicalDeliveryOfficeName,telephoneNumber,
           mail,givenName,initials,co,department,company,
           streetAddress,employeeID,mobile,userPrincipalName
    }
}		
...

Hope it will be useful to everyone...

License

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


Written By
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseUpdate Info in AD Pin
Júnior Pacheco9-Jul-18 5:22
professionalJúnior Pacheco9-Jul-18 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.