Directory Services

IADsContainer::Create

The IADsContainer::Create method sets up a request to create a directory object of the specified schema class and a given name in the container. The object is not made persistent until IADs::SetInfo is called on the new object. This allows for setting mandatory properties on the new object.

HRESULT Create( 
  BSTR bstrClass,
  BSTR bstrRelativeName,
  IDispatch** ppNewObject
);

Parameters

bstrClass
[in] Name of the schema class object to be created. The name is that returned from the IADs::get_Schema property method.
bstrRelativeName
[in] Relative name of the object as it is known in the underlying directory and identical to the one retrieved through the IADs::get_Name property method.
ppNewObject
[out] Indirect pointer to the IDispatch interface on the newly created object.

Return Values

This method supports the standard return values, including S_OK for a successful operation. For more information about error codes, see ADSI Error Codes.

Example Code [Visual Basic]

The following code example shows how to create a user object using IADsContainer::Create with IADs::SetInfo.

Dim cont As IADsContainer
Dim usr As IADsUser

On Error GoTo Cleanup
 
Set cont = GetObject("LDAP://OU=Sales,DC=Fabrikam,DC=com")
Set usr = cont.Create("user", "CN=jeffsmith")
usr.Put "samAccountName", "jeffsmith"
usr.SetInfo

Cleanup:
	If (Err.Number<>0) Then
		MsgBox("An error has occurred. " & Err.Number)
	End If
	Set cont = Nothing
	Set usr = Nothing

Example Code [C++]

The following code example shows how to create a user object.

HRESULT hr = S_OK;
IADsContainer *pCont = NULL;
IADs *pADs=NULL;
IDispatch *pDisp=NULL;
 
CoInitialize(NULL);
 
hr = ADsGetObject( L"WinNT://myMachine", 
				 IID_IADsContainer, 
				 (void**) &pCont );
if (!SUCCEEDED(hr) ) {goto Cleanup;}
 
hr = pCont->Create(CComBSTR("user"), CComBSTR("cn=JeffSmith"), &pDisp );
pCont->Release();
 
if( !SUCCEEDED(hr) ) { goto Cleanup; }
 
hr = pDisp->QueryInterface( IID_IADs, (void**) &pADs );
pDisp->Release();
 
if ( !SUCCEEDED(hr) ) { goto Cleanup;}
 
pADs->SetInfo(); // Commit

Cleanup:
	if(pADs)
		pADs->Release();

	if(pCont)
		pCont->Release();

	if(pDisp)
		pDisp->Release();

	return hr;

Requirements

Client: Included in Windows XP and Windows 2000 Professional.
Server: Included in Windows Server 2003 and Windows 2000 Server.
Redistributable: Requires Active Directory Client Extension on Windows NT 4.0 SP6a and Windows 95/98/Me.
Header: Declared in Iads.h.

See Also

IADsContainer, IADsContainer::Delete, IDispatch