How to: Open a Store on the Remote Server When Outlook is in Cached Exchange Mode

Office 2013 and later

Last modified: June 25, 2012

Applies to: Outlook

This topic contains a code sample in C++ that shows how to use the MDB_ONLINE flag to open a message store on the remote server when Microsoft Outlook 2010 or Microsoft Outlook 2013 is in Cached Exchange Mode.

Cached Exchange Mode permits Outlook 2010 and Outlook 2013 to use a local copy of a user's mailbox while Outlook 2010 or Outlook 2013 maintains an online connection to a remote copy of the user's mailbox on the remote Exchange server. When Outlook 2010 or Outlook 2013 is running in Cached Exchange Mode, by default, any MAPI solutions that log on to the same session are also connected to the cached message store. Any data that is accessed and any changes that are made are made against the local copy of the mailbox.

A client or service provider can override the connection to the local message store and open the store on the remote server by setting the bit for MDB_ONLINE in the ulFlags parameter when calling IMAPISession::OpenMsgStore. After the store has been successfully opened on the remote server for that session, you can use IMAPISession::OpenEntry to open items or folders on the remote store.

You cannot open an Exchange store in cached mode and in non-cached mode at the same time in the same MAPI session. If you have already opened the cached message store, you must either close the store before you open it with this flag, or open a new MAPI session where you can open the Exchange store on the remote server by using this flag.

The following code sample shows how to call IMAPISession::OpenMsgStore with the MDB_ONLINE flag set in the ulFlags parameter to open the default store on the remote server.

HRESULT HrRemoteMessageStore( 
    LPMDB* lppMDB) 
    HRESULT hRes = S_OK; 
    LPMAPITABLE pStoresTbl = NULL; 
    SRestriction sres = {0}; 
    SPropValue spv = {0}; 
    LPSRowSet pRow = NULL; 
    LPMDB lpTempMDB = NULL; 
    enum {EID,NUM_COLS}; 
    static SizedSPropTagArray(NUM_COLS,sptCols) = {NUM_COLS, 
    //Obtain the table of all the message stores that are available 
    hRes = lpMAPISession->GetMsgStoresTable(0, &pStoresTbl); 
    if (SUCCEEDED(hRes) && pStoresTbl) 
        //Set up restrictions for the default store 
        sres.rt = RES_PROPERTY;                                  //Comparing a property 
        sres.res.resProperty.relop = RELOP_EQ;                   //Testing equality 
        sres.res.resProperty.ulPropTag = PR_DEFAULT_STORE;       //Tag to compare 
        sres.res.resProperty.lpProp = &spv;                      //Prop tag and value to compare against 
        spv.ulPropTag = PR_DEFAULT_STORE;                        //Tag type 
        spv.Value.b   = TRUE;                                    //Tag value 
        //Convert the table to an array that can be stepped through 
        //Only one message store should have PR_DEFAULT_STORE set to true, so that only one will be returned 
        hRes = HrQueryAllRows( 
            pStoresTbl,                                          //Table to query 
            (LPSPropTagArray) &sptCols,                          //Which columns to obtain 
            &sres,                                               //Restriction to use 
            NULL,                                                //No sort order 
            0,                                                   //Max number of rows (0 means no limit) 
            &pRow);                                              //Array to return 
        if (SUCCEEDED(hRes) && pRow && pRow->cRows) 
            //Open the first returned (default) message store 
            hRes = lpMAPISession->OpenMsgStore( 
                NULL,                                                //Window handle for dialogs 
                pRow->aRow[0].lpProps[EID].Value.bin.cb,             //size and... 
                (LPENTRYID)pRow->aRow[0].lpProps[EID].Value.bin.lpb, //value of entry to open 
                NULL,                                                //Use default interface (IMsgStore) to open store 
                MAPI_BEST_ACCESS | MDB_ONLINE,                       //Flags 
                &lpTempMDB);                                         //Pointer to put the store in 
            if (SUCCEEDED(hRes) && lppMDB) lppMDB* = lpTempMDB; 
    if (pStoresTbl) pStoresTbl->Release(); 
    return hRes;