Directory Services

Setting Properties with Multiple Values

This topic shows how to set properties that contain multiple values using the following methods:

You can also set values using an indexed array, or as mentioned previously, you can append a single new value to a property that contains multiple values using the Add method.

When you set a property value, the data is saved in the property cache. To write the new data to the directory, call the CommitChanges method. For more information, see The Property Cache.

The following code example shows how to use the AddRange method.

[Visual Basic .NET]
Dim ent As New DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com")
ent.Properties("otherTelephone").AddRange(New Object() {"(425) 523 1462", "(523) 125 6321"})
ent.CommitChanges()
[C#]
DirectoryEntry ent = new DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com");
ent.Properties["otherTelephone"].AddRange(new object[] {"(425) 523 1462","(523) 125 6321"});
ent.CommitChanges();

The following code example shows how to use the Insert method.

[Visual Basic .NET]
Dim ent As New DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com")
ent.Properties("otherTelephone").Insert(2, "525 623 5423")
ent.CommitChanges()
[C#]
DirectoryEntry ent = new DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com");
ent.Properties["otherTelephone"].Insert(2, "525 623 5423");
ent.CommitChanges();

The following code example shows how to use an array to set a value on a property that contains multiple values.

[Visual Basic .NET]
Dim ent As New DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com")
ent.Properties("otherTelephone")(0) = "425 263 6234"
ent.CommitChanges()
[C#]
DirectoryEntry ent = new DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com");
ent.Properties["otherTelephone"][0] = "425 263 6234";
ent.CommitChanges();