Skip to content

Instantly share code, notes, and snippets.

@tobiasviehweger
Created May 5, 2015 10:11
Show Gist options
  • Save tobiasviehweger/e801e836d25df60be262 to your computer and use it in GitHub Desktop.
Save tobiasviehweger/e801e836d25df60be262 to your computer and use it in GitHub Desktop.
Enable "Display reminders and tasks from this folder in the To-Do bar" for PST Store programatically
private void EnableStoreTodosAndReminders(RDOStore store)
{
//Enable store for reminders
var reminders = store.Reminders;
reminders.EnableStoreReminders();
//Collection some folders we need
var rootFolder = store.RootFolder;
var taskFolder = store.GetDefaultFolder(rdoDefaultFolders.olFolderTasks);
var inbox = store.GetDefaultFolder(rdoDefaultFolders.olFolderInbox);
var rootFolders = rootFolder.Folders;
var reminderSearchFolder = reminders.SearchFolder;
//Create property PR_REM_ONLINE_ENTRYID, should be working without it, but well, while we're at it
rootFolder.Fields[MAPITags.PR_REM_ONLINE_ENTRYID] = reminderSearchFolder.EntryID;
//Now towards the todo search, that's a little more work => Start by creating the search folder
var todoSearch = rootFolders.AddSearchFolder("To-Do Search", rdoDefaultFolders.olFolderTasks);
var searchContainers = todoSearch.SearchContainers;
//Build up search criteria (from original search folder)
// => Exclude in folder (Outbox, Deleted, Drafts, Junk) => not necessary, we are only including tasks
var criteria = todoSearch.SearchCriteria;
var res = (RestrictionOr)criteria.SetKind(RestrictionKind.RES_OR);
var content1 = (RestrictionContent)res.Add(RestrictionKind.RES_CONTENT);
var content2 = (RestrictionContent)res.Add(RestrictionKind.RES_CONTENT);
content1.ulFuzzyLevel = ContentFuzzyLevel.FL_PREFIX | ContentFuzzyLevel.FL_IGNORECASE;
content1.ulPropTag = (int)MAPITags.PR_MESSAGE_CLASS_A;
content1.lpProp = "IPM.Task.";
content2.ulFuzzyLevel = ContentFuzzyLevel.FL_FULLSTRING | ContentFuzzyLevel.FL_IGNORECASE;
content2.ulPropTag = (int)MAPITags.PR_MESSAGE_CLASS_A;
content2.lpProp = "IPM.Task";
//Configure location & params
searchContainers.Add(taskFolder);
todoSearch.IsRecursiveSearch = true;
todoSearch.IsForegroundSearch = false;
//Start initial search
todoSearch.Start();
//Set some more properties that are necessary for Outlook to recognize this
// => Will actually lead to the checkbox "Display reminders and tasks from this folder in the To-Do bar" to be active
// => We need to create PR_ADDITIONAL_REN_ENTRYIDS_EX on the root level & the inbox level
// Persist element with id RSF_PID_TODO_SEARCH is necessary (0x8004) (Check with MFCMapi for more details)
var searchFolderId = StringUtil.StringToObjectArray(todoSearch.EntryID);
var idLen = Convert.ToUInt16(searchFolderId.Length);
var idLenAr = new byte[2];
idLenAr[0] = (byte)(idLen & 0xff);
idLenAr[1] = (byte)(idLen >> 8);
var header = StringUtil.StringToObjectArray("04801C000100");
var binaryContent = new object[header.Length + idLenAr.Length + searchFolderId.Length + 4];
//Join contents
Array.Copy(header, binaryContent, header.Length);
Array.Copy(idLenAr, 0, binaryContent, header.Length, idLenAr.Length);
Array.Copy(searchFolderId, 0, binaryContent, header.Length + idLenAr.Length, searchFolderId.Length);
//Finally, set the fields
rootFolder.Fields[MAPITags.PR_ADDITIONAL_REN_ENTRYIDS_EX] = binaryContent;
inbox.Fields[MAPITags.PR_ADDITIONAL_REN_ENTRYIDS_EX] = binaryContent;
//Clean up all COM leftovers
ComUtil.FinalReleaseObjects(content2, content1, res, criteria, searchContainers, todoSearch,
reminderSearchFolder, rootFolders, inbox, taskFolder, rootFolder, reminders);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment