Setting Properties with Multiple Values

This topic shows how to set multi-value properties using the following methods:

  • Add is a method of PropertyValueCollection that appends an additional property value to a multi-value property.
  • AddRange is a method of PropertyValueCollection that appends multiple values to a multi-value property.
  • Insert is a method of PropertyValueCollection that inserts a property value by its index position into a multi-value property. This position is only set on the client; when you commit it to the directory, there is no guarantee that it will be saved to this index position in Active Directory.

You can also set values using an indexed array.

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 multi-value property.

[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();