DirectoryServices Add/Remove Manager to Direct Report

N

news.microsoft.com

Greetings,

I am building a ASPX UI that will allow a HR representative manage the AD
Organization. I have a class that wraps a lot of AD work and I do things
like change password and update the GAL with Business information. I have
been able to write the code to get list of Direct Reports for a given user,
no problem. However, I am having trouble with how to set the 'manager' of a
AD user. Is it a property or a invoke function?

My code is simple like this...

public void AddDirectReport( string ManagerAlias, string UserAlias )

{

DirectoryEntry deManager = GetUser(ManagerAlias);

DirectoryEntry deDirectReport = GetUser(UserAlias);



this.SetProperty( deDirectReport, "manager", this.GetProperty( deManager,
"dn" ) );

deManager.Close();

deDirectReport.Close();

}



public void SetProperty(DirectoryEntry oDE, string PropertyName, string
PropertyValue)

{

//check if the value is valid, otherwise dont update

if(PropertyValue !=string.Empty)

{

//check if the property exists before adding it to the list

if(oDE.Properties.Contains(PropertyName))

{

oDE.Properties[PropertyName][0]=PropertyValue;

oDE.CommitChanges();

}

else

{

oDE.Properties[PropertyName].Add(PropertyValue);

oDE.CommitChanges();

}

}

}



I have tried passing just the user alias instead of the Dn, and I get a
'Constrain violation error'. The code above, actually does not throw an
error, but it does not add the Manager either. Of course, GetUser() and
SetProperty() and GetProperty() are other members of the class to get the
Directory Entry of a user.

I have tried passing an empty string as the property value to try to remove
an already established Manager, but does not seem to work. The account that
makes the AD connection is a domain admin so it has rights..plus my other AD
code is running fine with this account on my staging environment. I have
waited 15 min to see if DC has been replicated, but no change to the AD
users I am testing with.

I have found example code using VB and ADSI calls, but C# and
DirectoryService namespace is scarce. The VB stuff looks like I am doing
the correct calls, but maybe I am missing something.

Anyone have experience in doing this with C# will help me greatly.

Thanx

Micah
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top