Update the User Account

The properties that are updated are passed into this method, and only the non-null parameters are updated for this user principal object. The underlying DirectoryEntry object is retrieved to update the street address property, which exists only on the DirectoryEntry object.

        internal static bool UpdateUser(string user, string firstName, string lastName, string emailAddress, string telephone, string address)
        {
            // Create the PrincipalContext.
            PrincipalContext principalContext = null;
            try
            {
                principalContext = new PrincipalContext(ContextType.Domain, "fabrikam", "DC=fabrikam,DC=com");
            }
            catch (Exception e)
            {
                MessageBox.Show("Failed to create PrincipalContext. Exception: " + e);
                Application.Exit();
            }

            UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, user);
            if (userPrincipal != null)
            {
                if (firstName != null && firstName.Length > 0)
                    userPrincipal.GivenName = firstName;

                if (lastName != null && lastName.Length > 0)
                    userPrincipal.Surname = lastName;

                if (emailAddress != null && emailAddress.Length > 0)
                    userPrincipal.EmailAddress = emailAddress;

                if (telephone != null && telephone.Length > 0)
                    userPrincipal.VoiceTelephoneNumber = telephone;

                try
                {
                    userPrincipal.Save();
                }
                catch (Exception e)
                {
                    MessageBox.Show("Exception updating user object. " + e);
                    return false;
                }

                /***************************************************************
                 *   The below code demonstrates on how you can make a smooth 
                 *   transition to DirectoryEntry from AccountManagement namespace, 
                 *   for advanced operations.
                 ***************************************************************/
                if (userPrincipal.GetUnderlyingObjectType() == typeof(DirectoryEntry))
                {
                    DirectoryEntry entry = (DirectoryEntry)userPrincipal.GetUnderlyingObject();
                    if (address != null && address.Length > 0)
                        entry.Properties["streetAddress"].Value = address;
                    try
                    {
                        entry.CommitChanges();
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Exception modifying address of the user. " + e);
                        return false;
                    }
                }

                return true;
            }
            else
            {
                MessageBox.Show("Could not find the user [" + user + "]");
                return false;
            }
        }

See Also

Reference

System.DirectoryServices.AccountManagement

Concepts

About System.DirectoryServices.AccountManagement
Using System.DirectoryServices.AccountManagement

Send comments about this topic to Microsoft.

Copyright © 2008 by Microsoft Corporation. All rights reserved.