Platform SDK: MAPI |
The following sample code shows how to create a restriction that filters out all messages that do not contain the word "volleyball" in the subject line and were not sent to Sue from Sam. A tree of SRestriction structures is required, with the top node being an AND restriction implemented with an SAndRestriction structure. The three restrictions that are joined by the AND operation are a subobject restriction that searches for messages sent to Sue, a content restriction that searches for messages from Sam, and another AND restriction that searches for messages that have a subject containing "volleyball." Because PR_SUBJECT is not a required property, an exist restriction must be included.
This code uses dynamic allocation and initialization; it is possible to allocate and initialize statically as well. In the interest of brevity, the error checking that must occur following the allocation calls is not included in the sample.
HRESULT BuildRestriction (LPTSTR pszSent, LPTSTR pszFrom, LPTSTR pszSubjectText); { LPSRestriction pRest, pAndRes, pObjRes, pSubjAndRes; LPSPropValue pRecip, pSender, pSubject; HRESULT hResult; ULONG ulResCount = 3, ulSubjCount = 2 // Allocate and build AND restriction to join criteria hResult = MAPIAllocateMore (sizeof(SRestriction)*ulResCount, pRest, (LPVOID *)&pAndRes); pRest->rt = RES_AND; pRest->res.resAnd.cRes = ulResCount; pRest->res.resAnd.lpRes = pAndRes; // Allocate and build subobject restriction to search recipient list hResult = MAPIAllocateMore (sizeof(SRestriction), pRest, (LPVOID *)&pObjRes); pAndRes[0].rt = RES_SUBRESTRICTION; pAndRes[0].res.resSub.ulSubObject = PR_MESSAGE_RECIPIENTS; pAndRes[0].res.resSub.lpRes = pObjRes; // Allocate and build content restriction to look for recipient hResult = MAPIAllocateMore (sizeof(SPropValue), pRest, (LPVOID *)&pRecip); pObjRes->rt = RES_CONTENT; pObjRes->res.resContent.ulFuzzyLevel = FL_FULLSTRING | FL_IGNORECASE; pObjRes->res.resContent.ulPropTag = pRecip->ulPropTag = PR_DISPLAY_NAME; pObjRes->res.resContent.lpProp = pRecip; pRecip->Value.LPSZ = pszSent; // pszSent set to Sue // Allocate and build content restriction to look for sender hResult = MAPIAllocateMore (sizeof(SPropValue), pRest, (LPVOID *)&pSend); pAndRes[1].rt = RES_CONTENT; pAndRes[1].res.resContent.ulFuzzyLevel = FL_FULLSTRING | FL_IGNORECASE; pAndRes[1].res.resContent.ulPropTag = pSend->ulPropTag = PR_SENDER_NAME; pAndRes[1].res.resContent.lpProp = pSend; pSend->Value.LPSZ = pszName; // pszName set to Sam // Allocate and build AND restriction to look for subject hResult = MAPIAllocateMore (sizeof(SRestriction)*ulSubjCount, pRest, (LPVOID *)&pSubjAndRes); pRest->rt = RES_AND; pRest->res.resAnd.cRes = ulResCount; pRest->res.resAnd.lpRes = pAndRes; // Create an AND restriction to search for subject hResult = MAPIAllocateMore (sizeof(SPropValue), pRest, (LPVOID *)&pSubjAndRes); pAndRes[2].rt = RES_AND; pAndRes[2].res.resAnd.cRes = ulSubjCount; pAndRes[2].res.resAnd.lpRes = pSubjAndRes; // Exist restriction to check that PR_SUBJECT exists hResult = MAPIAllocateMore (sizeof(SPropValue), pRest, (LPVOID *)&pSubj); pSubjAndRes[0].rt = RES_EXIST; pSubjAndRes[0].res.resExist.ulReserved1 = 0; pSubjAndRes[0].res.resExist.ulReserved2 = 0; pSubjAndRes[0].res.resExist.ulPropTag = PR_SUBJECT; // Content restriction to check for "volleyball" in subject hResult = MAPIAllocateMore (sizeof(SPropValue), pRest, (LPVOID *)&pSubj); pSubjAndRes[1].res.resContent.ulFuzzyLevel = FL_SUBSTRING | FL_IGNORECASE; pSubjAndRes[1].res.resContent.ulPropTag = pSubj->ulPropTag = PR_SUBJECT; pSubjAndRes[1].res.resContent.lpProp = pSubj; pSubj->Value.LPSZ = pszSubjectText; return hResult; }