This section demonstrates how to add and remove attributes and their values.
If the attribute name and value are strings, you might be able to use slapi_entry_add_string(). The following example builds an entry first by setting the DN, then by adding attributes with string values.
#include "slapi-plugin.h"
int
test_create_entry()
{
Slapi_Entry * entry = NULL; /* Original entry */
entry = slapi_entry_alloc();
slapi_entry_set_dn(entry, slapi_ch_strdup("dc=example,dc=com"));
slapi_entry_add_string(entry, "objectclass", "top");
slapi_entry_add_string(entry, "objectclass", "domain");
slapi_entry_add_string(entry, "dc", "example");
/* Add code using the entry you created..... */
slapi_entry_free(entry);
return (0);
}
When working with values that already exist or with values that are not strings, you can use the following functions
Use slapi_entry_add_values_sv() to add new values to an attribute of an entry. This function returns an error when duplicates of the new values already exist.
Use slapi_entry_attr_merge_sv() to add new values to the existing values of a multivalued attribute. This function does not return an error when duplicates of the new values already exist.
The following example demonstrates slapi_entry_attr_merge_sv().
#include "slapi-plugin.h"
int
test_ldif()
{
Slapi_Entry * entry = NULL; /* Entry to hold LDIF */
Slapi_Value * values[2]; /* Attribute values */
entry = slapi_entry_alloc();
/* Add code to transform an LDIF representation into an entry.*/
/* Add a description by setting the value of the attribute.
* Although this is overkill when manipulating string values,
* it can be handy when manipulating binary values. */
values[0] = slapi_value_new_string("Description for the entry.");
values[1] = NULL;
if (slapi_entry_attr_merge_sv(entry,"description",values) != 0)
/* Merge did not work if processing arrives here. */ ;
slapi_entry_free(entry);
return (0);
}
If the attribute exists in the entry, slapi_entry_attr_merge_sv() merges the specified values into the set of existing values. If the attribute does not exist, slapi_entry_attr_merge_sv() adds the attribute with the specified values.
Remove attribute values with slapi_entry_delete_string() or slapi_entry_delete_values_sv().