HOWTO: Export Directory Information to a File Using BatchExport

ID: Q200154


The information in this article applies to:


SUMMARY

Exporting mailbox information about mailboxes on the messaging system may be required. You can do this programmatically using BatchExport() and passing a few parameters. This article contains the code that demonstrates how to use BatchExport() to write out the mailbox information for all users to the file Test.csv. The program takes two parameters: server and organization name.


MORE INFORMATION

The following code should be compiled as a console application using these libraries:




#include <windows.h>
#include <stdio.h>
#include <lmcons.h
#include <dapi.h>

void ReportDAPIEvent(DAPI_EVENT* pDAPIEvent);

void main(int argc, char* argv[])

{
   HRESULT hr = 0;
   BEXPORT_PARMS    BexportParms    = {0};
   LPBEXPORT_PARMS lpBexportParms  = {0};
   DAPI_HANDLE hDAPISession;
   DAPI_EVENT* pDAPIEvent = NULL;
   DAPI_PARMS DAPIParms = {0};
   char szAccount[UNLEN + MAX_COMPUTERNAME_LENGTH + 2];
   DWORD dwAccountLength = UNLEN + MAX_COMPUTERNAME_LENGTH + 1;
   if (2 > argc) {
      printf("\nDAPITest ExchangeServerName /O=Organization");
      return;
   }
   printf("\nExchange Server: %s", argv[1]);
    // start DAPI for this session
   //initialize the the DAPI Parms structure and the DAPI operation session
   DAPIParms.dwDAPISignature = DAPI_SIGNATURE;
   DAPIParms.dwFlags = DAPI_EVENT_ALL|DAPI_MODIFY_REPLACE_PROPERTIES|DAPI_RESTRICT_ACCESS ;
   DAPIParms.pszDSAName = argv[1];
   DAPIParms.pszBasePoint = NULL;
   DAPIParms.pszContainer = NULL;
   DAPIParms.pszNTDomain = argv[1];
   DAPIParms.pszCreateTemplate = NULL;
   DAPIParms.pAttributes = NULL;
   pDAPIEvent = DAPIStart(&hDAPISession, &DAPIParms);  //struct with DAPI params
   if(pDAPIEvent)
   {
        printf("\nDAPIStart() ERROR %08x - check app eventlog", pDAPIEvent->dwDAPIError);
        ReportDAPIEvent(pDAPIEvent);
    }
    else
        printf("\nDAPIStart() was successful");
   lpBexportParms = &BexportParms;
   lpBexportParms->dwDAPISignature = DAPI_SIGNATURE;
   lpBexportParms->dwFlags = DAPI_EXPORT_MAILBOX | DAPI_EXPORT_HIDDEN  | DAPI_EVENT_ALL |DAPI_EXPORT_SUBTREE ;
   lpBexportParms->pszExportFile = "TEST.CSV";
   lpBexportParms->pszBasePoint = argv[2];
   lpBexportParms->pszDSAName = argv[1];
   lpBexportParms->pszHomeServer = NULL;
   lpBexportParms->chColSep = DAPI_DEFAULT_DELIM;
   lpBexportParms->chQuote = DAPI_DEFAULT_QUOTE;
   lpBexportParms->chMVSep = DAPI_DEFAULT_MV_SEP;
   hr = BatchExport(lpBexportParms);
   DAPIEnd(&amp;hDAPISession);
   printf("\nEND PROGRAM");
}
void ReportDAPIEvent(DAPI_EVENT* pDAPIEvent)
{
    HANDLE hDAPIEventSource = RegisterEventSource(NULL, TEXT("MSExchangeDSImp"));
    ReportEvent(
         hDAPIEventSource,
         (WORD)EVENTLOG_ERROR_TYPE,
         0,
         pDAPIEvent->dwDAPIError,
         NULL,
         (WORD)pDAPIEvent->unSubst,
         0,
         pDAPIEvent->rgpszSubst,
         NULL);
    DAPIFreeMemory(pDAPIEvent);
    DeregisterEventSource(hDAPIEventSource);
     )

} 


REFERENCES

Another example of BatchExport is in the Dirysnc.c sample found on the Microsoft Developer Network Library.
For additional information, please see the following articles in the Microsoft Knowledge Base:

Q184268 HOWTO: Using DAPIRead
Q188960 HOWTO: Use BatchExport to Specify Which Attributes to Export
Q186333 HOWTO: Programmatically Get List of Exchange Servers in the Org

Additional query words: kbDSupport kbMsg kbEDK kbEDK550 kbEDK500 kbMAPI kbMAPI100


Keywords          : kbEDK kbMAPI kbMsg kbEDK500 kbEDK550 kbMAPI100 
Version           : WINDOWS:1.0,5.0,5.5
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: March 19, 1999