This took me well over an hour to figure out and even though I now know how to get around the problem, I still don’t know what’s causing it.

I have a piece of code that calls the DirectoryEntry.Rename(…) method. It does exactly what is says on the box, it renames the entry. (see here).

The first pitfall you’ll encounter is the input the method expects. The MSDN article states that it expects the new name, but that’s not how it works. You need to prefix it with CN= (or OU= in case of an Organizational Unit).

But now for the real issue.

Me method takes a SID (as string) as a parameter which is used to look up the entry like this:

	using (var targetGroup = new DirectoryEntry(string.Format("{0}", ConfigSettings.LdapPrefix, groupSid)))
	{
	}
 

Works great and does what needs to be done. But I also want to rename the targetGroup entry.

	using (var targetGroup = new DirectoryEntry(string.Format("{0}", ConfigSettings.LdapPrefix, groupSid)))
	{
		targetGroup.Rename(string.Format("CN={0}", newGroupName));
	}

But guess what…when you call it like this, it throws a NotImplementedException
As I said, I don’t know what is causing this (bug in the framework?), but there is a way around it:

	using (var targetGroup = new DirectoryEntry(string.Format("{0}", ConfigSettings.LdapPrefix, groupSid)))
	{
		var groupDn = AdUtil.GetProperty(targetGroup, "distinguishedName");
		using (var actualGroup = new DirectoryEntry(string.Format("{0}{1}", ConfigSettings.LdapPrefix, groupDn))
		{
			actualGroup.Rename(string.Format("CN={0}", newGroupName));
		}
	}
 

So here’s the trick, bind to the entry via its SID, then bind to it again with a new DirectoryEntry object using the distinguishedName of the previous object. It’s not pretty, but now you can rename your object without being slapped with a NotImplementedException.

Sander Harrewijnen

Author Sander Harrewijnen

Als ontwikkelaar mag ik graag problemen oplossen en nieuwe functionaliteit bouwen. Vanuit mijn creatieve kant hou ik me ook graag bezig met hoe iets bij de eindgebruiker aankomt. Je kunt bij mij dus ook terecht voor de opmaak en layout van een oplossing. Als je mij wilt afleiden, dan lukt dat altijd met een coole nieuwe gadget. Dat is namelijk mijn andere passie.

More posts by Sander Harrewijnen
28 October 2011

Leave a Reply