We are having issues using active directory and moving/renaming OUs. This only occurs when we replicate between two domain controllers. The exception we get is:
System.ServiceModel.FaultException: There is no such object on the server. (Exception from HRESULT: 0x80072030)
We get variations of this error message when we try to move and rename an OU in active directory. Here's the code in question:
PrincipalContext context = GetPrincipalContext();
using (UserPrincipal principal = UserPrincipal.FindByIdentity(context, IdentityType.Guid, id.ToString()))
{
if (principal == null)
{
throw new InvalidOperationException();
}
string oldEmail = principal.EmailAddress;
principal.EmailAddress = newEmail;
principal.Save();
DirectoryEntry entry = principal.GetUnderlyingObject() as DirectoryEntry;
DirectoryEntry targetDirectoryEntry = null;
string target = null;
// Access the underlying DirectoryEntry to rename it:
try
{
if (entry == null)
{
throw new InvalidOperationException();
}
entry.RefreshCache();
entry.Rename(string.Format("CN={0}", newEmail));
// Move the DirectoryEntry to the correct location.
target = BuildOrganizationalUnitName(newEmail);
targetDirectoryEntry = FindDirectoryEntry(target);
if (targetDirectoryEntry == null)
{
throw new InvalidOperationException();
}
entry.MoveTo(targetDirectoryEntry);
entry.CommitChanges();
}
catch (Exception e)
{
// do some logging
}
finally
{
if (entry != null)
{
entry.Dispose();
}
if (targetDirectoryEntry != null)
{
targetDirectoryEntry.Dispose();
}
}
}
So I have two questions:
You probably should commit the changes to the rename before trying to move it.
entry.Rename(string.Format("CN={0}", newEmail));
entry.CommitChanges(); // add this line
User contributions licensed under CC BY-SA 3.0