Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help!!Can Add user to Active Directory, but can Not update

Status
Not open for further replies.

faithful1

Programmer
Sep 27, 2002
49
0
0
US
Well, this is very strange to me. I can add a user to Active directory via a custom component. However, I can not update an existing user. This code is part of a component I am writing. I get the unknown error at the line
**user.Properties["description"].Add("New description");


public void UpdateMe()
{
string path = "LDAP://10.xxx.xxx.x";
DirectoryEntry entry = new DirectoryEntry
path,"\\Administrator","password");
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=Test User)";
SearchResult result = search.FindOne();
DirectoryEntry user = result.GetDirectoryEntry();
user.Properties["description"].Add("New description");
user.Properties["givenName"].Value = "New Name";
user.CommitChanges();
entry.Close();
}

 
Maybe try to copy the user you want to modify, modify the user object, remove the old one, add the modified one.
 
GMX-
Thanks for your response!
I found another way- populating the Directory Entry differently. It works. I still don't understand why the first one didn't work.

string path = "LDAP://10.xxx.xxx.x";
DirectoryEntry entry = new DirectoryEntry
path,"\\Administrator","password");

DirectorySearcher search = new DirectorySearcher(entry);
//I am using an emp id number for user SAMAccountName, so I will search for SAMAccountName and grab 'cn' to update.
search.Filter = "(SAMAccountName=999999)";
SearchResult result = search.FindOne();
//from result, grab the "cn" name
name="cn=" + result.Properties["cn"][0].ToString();
//Make sure the user is in the User group
DirectoryEntry group = entry.Children.Find("CN=Users");
//Look for a child in this group w/ "cn" name found
DirectoryEntry user = group.Children.Find(name,"user");
//once user is found,update it
if (result.Properties.Contains("telephoneNumber"))
{user.Properties["telephoneNumber"].Value =(dr["varFaxNumber"]);}
else
{user.Properties["telephoneNumber"].Add(dr["varPhoneNumber"]);}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top