Setting Properties on Directory Objects

This topic explains and provides code examples for setting properties with single values for directory objects.

Use the following methods to modify property values:

  • Add is a method of PropertyValueCollection that adds a value to a property.
  • Value is a property of PropertyValueCollection that sets a new value directly to the property for properties that contain a single value.

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 Add method.

[Visual Basic .NET]

Try
    Dim ent As New DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com")
    ent.Properties("otherTelephone").Add("(425) 523 1462")
    ent.CommitChanges()
Catch Exception1 As Exception
    ' If a COMException is thrown, then the following code example can catch the text of the error.
    ' For more information about handling COM exceptions, see Handling Errors.
    Dim COMEx As System.Runtime.InteropServices.COMException = CType(Exception1, System.Runtime.InteropServices.COMException)
    Console.WriteLine(COMEx.ErrorCode)
End Try

[C#]

try
{
    DirectoryEntry ent = new DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com");
    ent.Properties["otherTelephone"].Add("(425) 523 1462");
    ent.CommitChanges();
}
catch (Exception Exception1)
{
    // If a COMException is thrown, then the following code can catch the text of the error.
    // For more information about handling COM exceptions, see Handling Errors.
    System.Runtime.InteropServices.COMException COMEx = 
    (System.Runtime.InteropServices.COMException) Exception1;
    Console.WriteLine(COMEx.ErrorCode);
}

The following code example shows how to use the Value property.

[Visual Basic .NET]

Try
    Dim ent As New DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com")
    ent.Properties("sn").Value = "Barr"
    ent.CommitChanges()
Catch Exception1 As Exception
    ' If a COMException is thrown, then the following code example can catch the text of the error.
    ' For more information about handling COM exceptions, see Handling Errors.
    Dim COMEx As System.Runtime.InteropServices.COMException = CType(Exception1, System.Runtime.InteropServices.COMException)
    Console.WriteLine(COMEx.ErrorCode)
End Try

[C#]

try
{
    DirectoryEntry ent = new DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com");
    ent.Properties["sn"].Value = "Barr";
    ent.CommitChanges();
}
catch (Exception Exception1)
{
    // If a COMException is thrown, then the following code example can catch the text of the error.
    // For more information about handling COM exceptions, see Handling Errors.
    System.Runtime.InteropServices.COMException COMEx = 
        (System.Runtime.InteropServices.COMException) Exception1;
    Console.WriteLine(COMEx.ErrorCode);
}