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.

Related articles

  • Application Modernization
  • Digital Workspace
  • Cloud Infrastructure
  • ITTS (IT Transformation Services)
  • Managed Security Operations
  • Multi-Cloud Platform
  • Backup & Disaster Recovery
Visit our knowledge hub
Visit our knowledge hub
Sander Harrewijnen Developer

Let's talk!

Knowledge is key for our existence. This knowledge we use for disruptive innovation and changing organizations. Are you ready for change?

"*" indicates required fields

Your first name*
Your last name*