Previous | Next | Trail Map | The Basics | Directory Operations

Modifying Attributes

The DirContext(in the API reference documentation) interface contains methods for modifying the attributes and attribute values of objects in the directory.

Using a List of Modifications

One way to modify the attributes of an object is to supply a list of modification requests ( ModificationItem(in the API reference documentation)). Each ModificationItem consists of a numeric constant indicating the type of modification to make, and an Attribute(in the API reference documentation) describing the modification to make. The three types of modifications are:

Modifications are applied in the order in which they appear in the list. Either all of the modifications are executed, or none are.

The following code creates a list of modifications. It replaces the "mail" attribute's value with a value of "geisel@wizards.com", adds an additional value to the "telephonenumber" attribute, and removes the "jpegphoto" attribute.

// Specify the changes to make
ModificationItem[] mods = new ModificationItem[3];

// Replace mail attribute with new value
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
    new BasicAttribute("mail", "geisel@wizards.com"));

// Add additional value to "telephonenumber"
mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
    new BasicAttribute("telephonenumber", "+1 555 555 5555"));

// Remove jpegphoto
mods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
    new BasicAttribute("jpegphoto"));

After creating this list of modifications, you can supply it to the modifyAttributes()(in the API reference documentation) method as follows:

// Perform requested modifications on named object
ctx.modifyAttributes(name, mods);

Using Attributes

Alternatively, you can perform modifications by specifying the type of modification, and the attributes to which to apply the modification.

For example, the following line replaces the attributes (identified in orig) associated with name with those in orig:

ctx.modifyAttributes(name, DirContext.REPLACE_ATTRIBUTE, orig);
If name has other attributes, they remain unchanged.

Both of these uses of modifyAttributes() are demonstrated in the sample program, which modifies the attributes using a modification list, and then uses the second form of modifyAttributes() to restore the original attributes.


Previous | Next | Trail Map | The Basics | Directory Operations