Directory Services

Setting Properties on Directory Objects

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

Use the following methods to modify property values:

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.

[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 instructions about handling COM exceptions, see Handling Errors.
	System.Runtime.InteropServices.COMException COMEx = 
	(System.Runtime.InteropServices.COMException) Exception1;
	Console.WriteLine(COMEx.ErrorCode);
}
[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 can catch the text of the error.
	' For instructions 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

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

[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 can catch the text of the error.
	// For instructions about handling COM exceptions, see Handling Errors.
	System.Runtime.InteropServices.COMException COMEx = 
		(System.Runtime.InteropServices.COMException) Exception1;
	Console.WriteLine(COMEx.ErrorCode);
}
[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 can catch the text of the error.
	' For instructions 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