Renaming and moving objects using LinqToLdap

This article follows on from the one on OrganizationalUnit CRUD and deals with the two operations not covered: renaming and moving objects.  As discussed in that article, these operations are universal in that they are carried out on distinguishedNames rather than instances of some class representing a directory object.  In this sense, Delete is also universal but I left it in the CRUD article because otherwise it would’ve been CRU which wouldn’t have made sense to many people.  🙂

Renaming objects

This example builds on the code from the OrganizationalUnit CRUD article referred to earlier.  It requires an OU you can rename – I’ve called mine TestOu1 and I’m going to rename it to TestOu2.  Replace the contents of Program.cs with the following and enter the distinguishedName of your OU as the value of the originalDn variable.  Again, I’ve highlighted the lines that have changed from the earlier examples.

using System;
using System.Linq;
using LinqToLdap;

namespace BlogOuTest1
{
    class Program
    {
        static void Main(string[] args)
        {
            var config = new LdapConfiguration()
                .AddMapping(new OrganizationalUnitObjectMap());
            config.ConfigureFactory("dc1");

            var context = new DirectoryContext();

            var originalDn = "OU=TestOu1,OU=Tests,OU=Camelot,OU=Castles,DC=big,DC=wooden,DC=badger";
            var newDn = context.RenameEntry(originalDn, "TestOu2");

            var renamedOu = context.GetByDN(newDn);
            Console.WriteLine("objectGuid = {0}",
                renamedOu.objectGuid);

            Console.WriteLine("\r\nPress a key to continue...");
            Console.ReadKey(true);
        }
    }
}

As with Delete, RenameEntry takes the distinguishedName of the target object, rather than the object itself.  The second pararmeter is the new name.  This can be just the name, as shown, or with the  RDN prefix.  In this case, that would be

OU=TestOu2

.  In this case, I’ve used a different method to retrieve the object:

GetByDN<T>(distinguishedName)

which is more direct when you already have the distinguishedName.

Moving objects

Again, this example builds on the code from the OrganizationalUnit CRUD article referred to earlier.  It requires an OU you can move – I’m using TestOu2 from the previous step.  It also requires a target OU: I’ve created one called SubTest in the same OU as TestOu2:

Replace the contents of Program.cs with the following and enter the distinguishedName of your OU as the value of the originalDn variable and the dn of your parent OU as the value of the newParentDn variable.  Again, I’ve highlighted the lines that have changed from the earlier examples.

using System;
using System.Linq;
using LinqToLdap;

namespace BlogOuTest1
{
    class Program
    {
        static void Main(string[] args)
        {
            var config = new LdapConfiguration()
                .AddMapping(new OrganizationalUnitObjectMap());
            config.ConfigureFactory("dc1");

            var context = new DirectoryContext();

            var originalDn = "OU=TestOu2,OU=Tests,OU=Camelot,OU=Castles,DC=big,DC=wooden,DC=badger";
            var newParentDn = "OU=SubTest,OU=Tests,OU=Camelot,OU=Castles,DC=big,DC=wooden,DC=badger";
            var newDn = context.MoveEntry(originalDn, newParentDn);

            var movedOu = context.GetByDN(newDn);
            Console.WriteLine("objectGuid = {0}",
                movedOu.objectGuid);

            Console.WriteLine("\r\nPress a key to continue...");
            Console.ReadKey(true);
        }
    }
}

As with RenameEntry, MoveEntry takes the distinguishedName of the target object, rather than the object itself.  The second pararmeter is the distinguishedName of the new parent OU.

Summary

These operations complete the set of operations that you can carry out on directory objects using LinqToLdap.  The same commands work with other objects such as users and groups.