3.4 Modifying Entry Properties

This section contains information about how to modify the attributes of an existing entry inside a directory using classes provided by Novell.Directory.Ldap namespace. To modify the attributes of an existing entry, use the Modify function of LdapConnection class.

Modifying an entry attributes involves three steps:

  1. Create an LdapModification object for each of the attributes to be modified using an attribute with a new value (in case of add/replace) and the modification type, for example, add, replace or delete.

  2. Create an LdapModification array with all the LdapModification objects created above.

  3. Call the LdapConnection.Modify method to modify the entry attributes

Example

// C# Library namespace
using Novell.Directory.Ldap;

// Creating an LdapConnection instance 
LdapConnection ldapConn= new LdapConnection();

//Connect function will create a socket connection to the server
ldapConn.Connect(ldapHost,ldapPort);

//Bind function will bind the user object Credentials to the server
ldapConn.Bind(userDN,userPasswd);

ArrayList modList = new ArrayList();
String desc = "This object belongs to test user";

// Add a new value to the description attribute
LdapAttribute attribute = new LdapAttribute( "description", desc);
modList.Add( new LdapModification(LdapModification.ADD, attribute));

//Replace the existing email with the new email value
attribute = new LdapAttribute( "mail", "James_Smith@Acme.com");
modList.Add( new LdapModification(LdapModification.REPLACE,
attribute));

LdapModification[] mods = new LdapModification[modList.Count]; 
Type mtype=Type.GetType("Novell.Directory.LdapModification");
mods = (LdapModification[])modList.ToArray(typeof(LdapModification));

//Modify the entry in the directory
ldapConn.Modify ( dn, mods );