Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Creating a Record and Adding an Attribute

The sample code in Listing 3-4 demonstrates how to create a record, open it, and add an attribute to it. The sample code opens an Open Directory session and gets an Open Directory reference. Then it calls its MyOpenDirNode routine and passes to it the address of the node reference (nodeRef) that it has allocated. The MyOpenDirNode routine is described in the section “Opening and Closing a Node.”

The sample code then calls its CreateRecord routine and passes to it the node reference (nodeRef) obtained by calling its MyOpenDirNode routine.

The CreateRecord routine calls dsDataNodeAllocateString to allocate a data node (recName) containing the string “NewUserRecordName” and another data node (recType) specifying kDSStdRecordTypeUsers as the record type for the record that is to be created.

Then the CreateRecord routine then calls dsCreateRecordAndOpen, passing to it the node reference created when dsOpenDirService was called, recName, recType, and the address of a record reference value (recRef) initialized to zero. If dsCreateRecordAndOpen returns successfully, recRef will contain a record reference that the CreateRecord routine will use to add an attribute for the record.

The CreateRecord routine then calls dsDataNodeAllocateString to allocate a data node (attrName) containing kDS1AttrDistinguishedName. It also calls dsDataNodeAllocateString to allocate a data node containing the string “User Record’s Display Name”, which will be set as the value of the attribute.

To add the attribute and set its value, the CreateRecord routine calls dsAddAttribute. It then cleans up by calling dsCloseRecord to close the record and dsDataNodeDeAllocate to reclaim the memory associated with attrName, recType, and recName.

When the CreateRecord routine returns, the sample code in Listing 3-4 calls dsCloseDirNode to close the node that it opened in order to create and open the record.

Listing 3-4  Creating and opening a record and adding an attribute

void main ( )
{
    long dirStatus = eDSNoErr;
    tDirNodeReference nodeRef = NULL;
    dirStatus = dsOpenDirService( &gDirRef );
    if ( dirStatus == eDSNoErr )
    {
        dirStatus = MyOpenDirNode( &nodeRef );
        if ( dirStatus == eDSNoErr )
        {
            CreateRecord( nodeRef );
            dsCloseDirNode( nodeRef );
        }
    }
    if ( gDirRef != NULL )
    {
        dirStatus = dsCloseDirService( gDirRef );
    }
}
void CreateRecord ( const tDirNodeReference inDirNodeRef )
{
    long dirStatus = eDSNoErr;
    tDataNodePtr recName = NULL;
    tDataNodePtr recType = NULL;
    tDataNodePtr attrName = NULL;
    tDataNodePtr attrValue = NULL;
    tRecordReference recRef = NULL;
    recName = dsDataNodeAllocateString( gDirRef, "NewUserRecordName"  );
    if ( recName != NULL )
    {
        recType = dsDataNodeAllocateString( gDirRef, kDSStdRecordTypeUsers  );
        if ( recType != NULL )
        {
            dirStatus = dsCreateRecordAndOpen( inDirNodeRef, recType,  recName,  &recRef );
            if ( dirStatus == eDSNoErr )
            {
                attrName = dsDataNodeAllocateString(gDirRef,  kDS1AttrDistinguishedName );
                if ( attrName != NULL )
                {
                    attrValue = dsDataNodeAllocateString( gDirRef,  "User Record's  Display Name");
                    if ( attrValue != NULL )
                    {
                        dirStatus = dsAddAttribute(recRef, attrName,  NULL,  attrValue );
                        dirStatus = dsDataNodeDeAllocate( gDirRef,  attrValue );
                        attrValue = NULL;
                    }
                    dirStatus = dsDataNodeDeAllocate( gDirRef, attrName  );
                    attrName = NULL;
                }
                dirStatus = dsCloseRecord( recRef );
                recRef = NULL;
            }
            dirStatus = dsDataNodeDeAllocate( gDirRef, recType );
            recType = NULL;
        }
        dirStatus = dsDataNodeDeAllocate( gDirRef, recName );
        recName = NULL;
    }
} // CreateRecord

Note that for this example to work, it would have to be run by a root process on the local NetInfo domain, or by a user process that has called dsDoDirNodeAuth with the inDirNodeAuthOnlyFlag parameter set to FALSE to get permission to make this change.



< Previous PageNext Page > Hide TOC


Last updated: 2007-01-08




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice