0

I'm trying to create a new User in Active Directory from a VB.NET application. Most fields will be identical to an already existing "template" user, except things like Name, SurName, Email, SamAccountName, etc.

So I want to copy or clone this template User, assign the few fields with a new/different value and then save this new user in Active Directory. I'd like to avoid having to manually assign who-knows how many properties from my template to the new User and maybe forget something along the way..

Is there not a way to do that? I found something about using DirectoryEntry.CopyTo(), but I simply get a "Not implemented" error, and anyway I doubt this is the right direction (Unsure how to use this class together with a UserPrincipal object)

Should I be using a different class than System.DirectoryServices.AccountManagement.UserPrincipal to save my new user in the AD? Do I have the wrong approach?

Any help will be appreciated :)

1 Answer 1

1

If you look at the source code for DirectoryEntry.CopyTo, it calls:

newParent.ContainerObject.CopyHere(Path, newName)

And ContainerObject is of the type IADsContainer. If you look at the documentation for IADsContainer.CopyHere, it says (under Remarks):

The providers supplied with ADSI return the E_NOTIMPL error message.

ADSI is "Active Directory Service Interfaces". So the short answer is that it just won't work with AD objects.

There is no way to do what you want. You will have to manually assign each attribute you want to copy.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer. Allow me to ask: Even if what I want isn't possible via DirectoryEntry.CopyTo, how can you be confident there isn't another way to achieve this (other than assigning each attribute manually)? I admit I'm a bit lost in the whole .NET documentation concerning Active Directory, which object I should use use, etc.
Yes, I am confident there is no way to copy an AD object without writing the code yourself.
I wasn't asking if you were confided, I wanted to know why you're confident :P How do you actually navigate the documentation and come up with this definitive answer. How can you tell there's no other copy or clone method somewhere? I guess you just know from experience, and by having a good understanding of the hierarchy/inheritance of these classes... Anyway, thanks again :)
Sorry, I missed the "how". But yes, I have looked at the documentation quite a bit and I've never seen a method that allows it.
I wrote a whole article about getting better performance when programming with AD if you're interested.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.