views:

314

answers:

1

Hi,

I've been thrust into the deep end with Active Directory (I'm a web developer, go fig) I have a user that I've changed first/last name on but the Full Name hasn't changed which is causing issues with sharing off a BCM database. How do I refresh it so the Full Name is updated.

I have no idea how AD works but for some reason the higher ups have decided it's my job.

Any help would be really appreciated.

Thanks!

+2  A: 

Are you on .NET 3.5 ? If so, go check out this MSDN article: Managing Directory Security Principals in the .NET Framework 3.5.

Check out this CodeProject article: Howto: (Almost) Everything In Active Directory via C#

Check out this list of Code Samples for System.DirectoryServices on MSDN.

If you're serious about Active Directory Programming in C# or VB.NET, go buy this book:

The .NET Developer's Guide to Directory Services Programming

alt text

Updating the "full name" (really: DisplayName) should be as easy as (on .NET 3.5):

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "yourDomain");

UserPrincipal up = UserPrincipal.FindByIdentity(ctx, "(your user name)");

if (up != null)  // we found the user
{
   up.DisplayName = "new display name for your user";
   up.Save();
}

That's it! :-)

Mind you : you need to pass in the NetBIOS domain name (e.g. "MICROSOFT") - not a DNS-style name (microsoft.com) to the constructor of the PrincipalContext.

Hope this helps!

Marc

UPDATE for .NET 2.0:

Here's the code if you're running on .NET 2.0 and need to update the "displayName" for a user, given his "SAMAccountName" (his username without the domain, basically):

// set the root for the search
DirectoryEntry root = new DirectoryEntry("LDAP://dc=yourcompany,dc=com");

// searcher to find user in question
DirectorySearcher ds = new DirectorySearcher(root);

// set options
ds.SearchScope = SearchScope.Subtree;
ds.Filter = string.Format("(&(sAMAccountName={0})(objectCategory=Person))", yourUserName);

// do we find anyone by that name??
SearchResult result = ds.FindOne();

if (result != null)
{
    // if yes - retrieve the full DirectoryEntry for that user
    DirectoryEntry userEntry = result.GetDirectoryEntry();

    // set the "displayName" property to the new value
    userEntry.Properties["displayName"].Value = yourNewUserFullName;

    // save changes back to AD store
    userEntry.CommitChanges();
}
marc_s
Thanks, I've accepted it but not tried it, .net 3.5 isn't installed and I've been told not to run any code or do anything with it. So basically I have a problem that I've been told to fix and then told I can't fix.Again thanks for your help.
digital
what version of .NET are you running? 2.0 ? I'll update my post with a sample for the 2.0 version, too - it's just a bit more involved than the nice 3.5 version....
marc_s