Click here to Skip to main content
15,897,315 members
Articles / Desktop Programming / MFC

Large Address book in MAPI

Rate me:
Please Sign up or sign in to vote.
4.31/5 (8 votes)
7 Dec 20048 min read 120.7K   1.1K   38  
Explains how to display the list of users in a Global Address Book in a large organization using a Virtual List Control.
//MapiUtils.cpp


#include "stdafx.h"
#include "MAPIListCtrl.h"
#include "MapiUtils.h"


MapiUtils::MapiUtils()
{
	m_pSession = NULL;
	m_pGAL = NULL;
	m_pAddrBook = NULL;
	m_pContentsTable = NULL;

	//Get all the profile details in the local machine
	LoadProfileDetails();
}

MapiUtils::~MapiUtils()
{
}

HRESULT MapiUtils::StartandLogon(LPSTR szProfileName)
{

	HRESULT        hr             = S_OK;
	ULONG             cbeid    = 0L;
	LPENTRYID         lpeid    = NULL;
	ULONG             ulObjType;

	hr = MAPILogonEx (NULL, szProfileName, NULL, MAPI_EXTENDED| MAPI_NEW_SESSION| 
	  MAPI_LOGON_UI| MAPI_EXPLICIT_PROFILE,&m_pSession);
	if (FAILED (hr)) 
	{
		return E_FAIL;
	}

	SizedSPropTagArray ( 8, sptCols ) = { 8,
									PR_ENTRYID,PR_DISPLAY_NAME,PR_ACCOUNT,
									PR_OBJECT_TYPE,PR_OFFICE_LOCATION,PR_COMPANY_NAME, 
									PR_EMAIL_ADDRESS,PR_OFFICE_TELEPHONE_NUMBER};



	hr = m_pSession->OpenAddressBook(NULL,NULL,AB_NO_DIALOG,&m_pAddrBook);
	if(FAILED(hr))
	{
		StopandLogoff();
		return E_FAIL;
	}

	if ( FAILED ( hr = HrFindExchangeGlobalAddressList ( m_pAddrBook,
															   &cbeid,
															   &lpeid ) ) )
	{
		StopandLogoff();
		return E_FAIL;
	}

	if(FAILED(hr = m_pAddrBook->OpenEntry((ULONG) cbeid,
				  (LPENTRYID) lpeid,
				  NULL,
				  MAPI_BEST_ACCESS,
				  &ulObjType,
				 (LPUNKNOWN *)&m_pGAL)))
	{
		StopandLogoff();
		return E_FAIL;
	}
	if ( ulObjType != MAPI_ABCONT )
	{
		StopandLogoff();
		return E_FAIL;
	}

	//Free the entry id buffer
	::MAPIFreeBuffer(lpeid);
	if(FAILED(hr = m_pGAL->GetContentsTable(0L, &m_pContentsTable)))
	{
		StopandLogoff();
		return E_FAIL;
	}

	if( FAILED(hr = m_pContentsTable->SetColumns((SPropTagArray*) &sptCols,0)))
	{
		StopandLogoff();
		return E_FAIL;
	}


	return S_OK;
}

HRESULT MapiUtils::StopandLogoff()
{
	if(m_pContentsTable)
	{
		m_pContentsTable->Release();
		m_pContentsTable = NULL;
	}
	if(m_pGAL)
	{
		m_pGAL->Release();
		m_pGAL = NULL;
	}

	if(m_pAddrBook)
	{
		m_pAddrBook->Release();
		m_pAddrBook = NULL;
	}

	if(m_pSession != NULL)
	{
		m_pSession->Logoff (0, 0, 0);
		m_pSession->Release ();
		m_pSession = NULL;
	}
	return S_OK;

}

HRESULT MapiUtils::SetFilter(LPCSTR pszFilter)
{
   ULONG             cbeid    = 0L;
   LPENTRYID         lpeid    = NULL;
   HRESULT           hr       = S_OK;
   SPropValue        spvDisplay, spvAddrType;

   spvDisplay.ulPropTag = PR_DISPLAY_NAME;
   spvDisplay.Value.lpszA = (char *)pszFilter;
   spvAddrType.ulPropTag = PR_DISPLAY_TYPE;
   spvAddrType.Value.l = DT_MAILUSER|DT_DISTLIST;
   
   SRestriction      sres,srlevel1[2];

   if(strcmpi(pszFilter,"")!=0)
   {
	   //Create Restriction
	   sres.rt = RES_AND;
	   sres.res.resAnd.cRes = 1;
	   sres.res.resAnd.lpRes =srlevel1;
	   srlevel1[0].rt = RES_CONTENT;
	   srlevel1[0].res.resContent.ulFuzzyLevel = FL_PREFIX|FL_IGNORECASE;
	   srlevel1[0].res.resContent.ulPropTag = PR_DISPLAY_NAME;
	   srlevel1[0].res.resContent.lpProp = &spvDisplay;

	   hr = m_pContentsTable->Restrict(&sres,0);
   }
   else
   {
 	    hr = m_pContentsTable->Restrict(NULL,0);
   }
   return hr;
}

HRESULT MapiUtils::LoadProfileDetails()
{
	HRESULT                         hr; 
	LPMAPITABLE                   pTable = NULL; 
	LPSRowSet                      pRows = NULL; 
	bool                               found=false; 
	SizedSPropTagArray(1, Columns) ={1, {PR_DISPLAY_NAME}}; 
    LPPROFADMIN pProfAdmin = NULL; // Pointer to IProfAdmin object

	CString l_ProfileName;

	hr = MAPIAdminProfiles(0, &pProfAdmin); 
    if (!FAILED(hr)) 
    { 
          // Get the ProfileTable - Contains all profiles 
          hr = pProfAdmin->GetProfileTable(0, &pTable); 
          if (FAILED(hr)) 
          { 
               //Error initializing profile table
          } 
    } 

	hr = HrQueryAllRows(pTable,              //pointer to table of pointers 
			  (LPSPropTagArray) &Columns, //list of columns we will get 
			  NULL,                                  //filter NULL. We need all rows
			  NULL,                                   //we don�t need any sorting here 
			  0,                                        //retrieve all rows that match 
			  &pRows);                              //pointer to resulting table 


	if (!FAILED(hr) && pRows->cRows>0) //result of HrQueryAllRows 
	{ 
		  found=true; 
		  for(int i=0;i<pRows->cRows;i++)
		  {
			  l_ProfileName = pRows->aRow[i].lpProps->Value.lpszA;
			  m_ProfileList.Add(l_ProfileName);
		  }
	} 
//Cleanup 
   if (pRows) FreeProws(pRows); 
   if (pTable) pTable->Release(); 
   if (pProfAdmin) pProfAdmin->Release(); 

   return S_OK;

}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions