Directory Services

Modifying Attributes with the IDirectoryObject Interface

In addition to IADs::Put and IADs::PutEx, you can use the IDirectoryObject::SetObjectAttributes method to modify attribute values. To use this method, you must fill in an ADS_ATTR_INFO structure for each attribute to modify.

The IDirectoryObject::SetObjectAttributes method enables you to modify both single-valued and multi-valued attributes. This function provides similar operational controls, such as clear, append, delete, and update, to those found in the IADs::PutEx method. The control constants include:

Specifying ADS_ATTR_UPDATE will trigger a server side operation that can be resource-intensive. An example would be to initiate the operation to update a long list of group membership. In general, refrain from using this operation unless the modification involves a small number of attributes in the directory. To modify a long list of group memberships, the more efficient approach would be to read the list from the underlying directory, make modifications, and store the updated list back to the directory.

Note  Like IADs::Put and IADs::PutEx with IADs::SetInfo, the attribute changes are either completely committed or discarded in Active Directory. If one or more of the modifications are not allowed and therefore not able to be performed, then none of the collective modifications made to the attributes are committed to the directory.

Example

The following code example shows how to modify both single and multi-valued attributes with the IDirectoryObject::SetObjectAttributes method.

[C++]
HRESULT hr;
LPCWSTR pwszADsPath = L"LDAP://CN=Jeff Smith,OU=Sales,DC=Fabrikam,DC=com";
IDirectoryObject *pDirObject = NULL;

// Bind to the object.
hr = ADsGetObject(pwszADsPath, IID_IDirectoryObject, (void**)&pDirObject);
if(SUCCEEDED(hr))
{ 
	ADSVALUE adsvFaxNumber;
	ADSVALUE rgadsvOtherTelephones[2];
	 
	// Set the new FAX number.
	adsvFaxNumber.dwType = ADSTYPE_CASE_IGNORE_STRING; 
	adsvFaxNumber.CaseIgnoreString = L"425-707-9790";

	// Set the first telephone number.
	rgadsvOtherTelephones[0].dwType = ADSTYPE_CASE_IGNORE_STRING;
	rgadsvOtherTelephones[0].CaseIgnoreString = L"425-707-9791";

	// Set the second telephone number.
	rgadsvOtherTelephones[1].dwType = ADSTYPE_CASE_IGNORE_STRING;
	rgadsvOtherTelephones[1].CaseIgnoreString = L"425-707-9792";

	ADS_ATTR_INFO attrInfo[2];

	// Setup the facsimileTelephoneNumber attribute data.
	attrInfo[0].pszAttrName = L"facsimileTelephoneNumber";
	attrInfo[0].dwControlCode = ADS_ATTR_UPDATE;
	attrInfo[0].dwADsType = adsvFaxNumber.dwType;
	attrInfo[0].pADsValues = &adsvFaxNumber;
	attrInfo[0].dwNumValues = 1;

	// Setup the otherTelephone attribute data.
	attrInfo[1].pszAttrName = L"otherTelephone";
	attrInfo[1].dwControlCode = ADS_ATTR_UPDATE;
	attrInfo[1].dwADsType = rgadsvOtherTelephones[0].dwType;
	attrInfo[1].pADsValues = rgadsvOtherTelephones;
	attrInfo[1].dwNumValues = sizeof(rgadsvOtherTelephones)/sizeof(ADSVALUE);

	DWORD dwReturn;
 
	// Set the new attribute values.
	hr = pDirObject->SetObjectAttributes(attrInfo, 
		sizeof(attrInfo)/sizeof(ADS_ATTR_INFO), 
		&dwReturn);

	pDirObject->Release();
}