HOWTO: Create an Organizational Forms Library Programmatically

ID: Q194976

The information in this article applies to:

SUMMARY

When deploying forms for Microsoft Exchange in a worldwide organization, it may be necessary to create several Organizational Forms Libraries in different languages much like the administrator program does. This is done using the MAPI function CreateFolder(). However, it is necessary to have very special privileges to get it to work. These special privileges can only be obtained by opening the store with an EntryId created using CreateStoreEntryID().

MORE INFORMATION

The sample code in this article does the following:

Once you have the pointer with the correct permissions, you can use it for standard calls such as CreateFolder(), SetProps(), and SaveChanges().

The additional libraries required to compile and link this Win32 application sample are as follows:

You will need to modify the code marked by "TO DO" to reference the appropriate Exchange Organization, Site and Server Name. You will also need to be logged into the Windows NT machine under a domain account that has permissions on the Exchange Server's Organization, Site and Configuration objects:

   #include <edk.h>

   LPMAPISESSION         lpMapiSession = NULL;
   LPMDB                 lpPubStore = NULL;
   LPMAPIFOLDER          lpPublicFolder1 = NULL,
                         lpFormFolder = NULL;
   LPMAPITABLE           lpContentTable = NULL;
   LPSRowSet             lpRows = NULL;
   ULONG                 rCount = 0, i = 0;
   HRESULT               hr;
   MAPIINIT_0            MapiInit;
   char                  lngName[100];

   static SizedSPropTagArray( 2L, PropFolderName) =
          { 2L, {PR_DISPLAY_NAME,0x3fe90003}};

   int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE,
                      LPSTR pszCmd, int nCmdShow)
   {
      MapiInit.ulVersion = 0;
      MapiInit.ulFlags = MAPI_NT_SERVICE;

      hr = MAPIInitialize( &MapiInit);
      if (!HR_SUCCEEDED(hr))
      {
         MessageBox(0l,"MAPIInitialize error","Error",MB_OK);
         return -1;
      }

      hr = MAPILogonEx( 0, (LPTSTR)NULL, (LPTSTR)"",
                        MAPI_NEW_SESSION | MAPI_EXTENDED | MAPI_LOGON_UI,
                        &lpMapiSession);
      if (!HR_SUCCEEDED(hr))
      {
         MessageBox(0l,"MAPILogonEx error","Error",MB_OK);
         return -1;
      }

      hr = HrOpenExchangePublicStore( lpMapiSession, &lpPubStore);
      if (!HR_SUCCEEDED(hr))
      {
         MessageBox(0l,"HrOpenExchangePublicStore error","Error",MB_OK);
         return -1;
      }

      LPEXCHANGEMANAGESTORE   pManageStore = NULL;
      ULONG                   cbeid;
      LPENTRYID               lpeid = NULL;
      char                    szServerDN[500];
      ULONG                   ulFlags;

      ulFlags = LOGOFF_PURGE;
      hr = lpPubStore->QueryInterface(IID_IExchangeManageStore,
                                      (LPVOID  *) &pManageStore);
      if (hr != SUCCESS_SUCCESS)
      {
         lpPubStore->StoreLogoff(&ulFlags);
         MessageBox(0,"QueryInterface error","Error",MB_OK);
         return -1;
      }

      // TO DO:  Create Server DN
      sprintf(szServerDN, "/o=%s", "MyOrganization");
      sprintf(szServerDN, "%s/ou=%s",szServerDN, "MySite");
      sprintf(szServerDN, "%s/cn=Configuration/cn=Servers/cn=%s",
                 szServerDN, "MyServer");
      sprintf(szServerDN, "%s/cn=Microsoft Public MDB",szServerDN);

      if (FAILED(hr = pManageStore->CreateStoreEntryID(szServerDN, NULL,
                    OPENSTORE_USE_ADMIN_PRIVILEGE | OPENSTORE_PUBLIC |
                    OPENSTORE_TAKE_OWNERSHIP | OPENSTORE_HOME_LOGON,
                    &cbeid, &lpeid)))
      {
         MessageBox(0l,"CreateStoreEntyrID error","Error",MB_OK);
         return - 1;
      }

      ulFlags = LOGOFF_PURGE;
      hr = lpPubStore->StoreLogoff(&ulFlags);

      if (hr != SUCCESS_SUCCESS)
      {
         MessageBox(0l,"StoreLogoff error","Error",MB_OK);
         return -1;
      }

      if (FAILED(hr = lpMapiSession->OpenMsgStore(0L, cbeid, lpeid,
                         NULL, MAPI_DEFERRED_ERRORS | MDB_WRITE,
                         &lpPubStore)))
      {
         MessageBox(0l,"OpenMsgStore error","Error",MB_OK);
         return -1;
      }

      // Property value: "Registries" subtree EID
      SPropValue     *pvalFR_EID      = NULL;

      ULONG           ul;
      SPropTagArray   tagaFR_EID      = { 1,
                                        { PR_EFORMS_REGISTRY_ENTRYID } };

      hr = lpPubStore -> GetProps (&tagaFR_EID, fMapiUnicode,
                                   &ul, &pvalFR_EID);
      if (! HR_SUCCEEDED (hr))
      {
         printf("GetProps error \n");
         return -1;
      }

      // Open the Registries subtree. The registry folder should be
      // created under this subtree.
      hr = lpPubStore -> OpenEntry (pvalFR_EID -> Value.bin.cb,
                              (LPENTRYID) pvalFR_EID -> Value.bin.lpb,
                              NULL, MAPI_MODIFY, & ul,
                              (LPUNKNOWN *) & lpPublicFolder1);

      if (! HR_SUCCEEDED (hr))
      {
         printf("OpenEntry error \n");
         return -1;
      }

      hr = lpPublicFolder1->CreateFolder(FOLDER_GENERIC,
                                  "TestFormLibrary", NULL, NULL,
                                  OPEN_IF_EXISTS | MAPI_DEFERRED_ERRORS,
                                  &lpFormFolder);

      if (!HR_SUCCEEDED(hr))
      {
         MessageBox(0l,"CreateFolder error","Error",MB_OK);
         return -1;
      }

      SPropValue lpPropValueArray2[3];

      lpPropValueArray2[0].ulPropTag   = PR_EFORMS_LOCALE_ID;
      lpPropValueArray2[0].Value.ul    =
               MAKELCID (MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_EIRE),
                         SORT_DEFAULT);
      lpPropValueArray2[1].ulPropTag   = PR_DISPLAY_NAME;
      lpPropValueArray2[1].Value.LPSZ  = "Forms Library (English-Irish)";
      lpPropValueArray2[2].ulPropTag   = PR_PUBLISH_IN_ADDRESS_BOOK;
      lpPropValueArray2[2].Value.b     = FALSE;

      if (FAILED(hr = lpFormFolder->SetProps(3,lpPropValueArray2,NULL)))
      {
         MessageBox(0l,"Setprops error","Error",MB_OK);
         return -1;
      }

      if (FAILED(hr = lpFormFolder->SaveChanges(KEEP_OPEN_READWRITE)))
      {
         MessageBox(0l,"SaveChanges error","Error",MB_OK);
         return -1;
      }

      if (lpMapiSession)
      {
         lpMapiSession->Logoff(0, 0, 0);
         ULRELEASE(lpMapiSession);
      }
      MAPIUninitialize();
      return 0 ;
   }

Additional query words:
Keywords          : kbMsg kbEDK550 kbMAPI100 
Version           : WINDOWS:1.0,5.0,5.5
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: November 1, 1998