Searching user tasks by task.body text

246 views Asked by At

Good morning all! I have spent a good bit of time searching and reading forums about this, but I can't seem to find a solution. Any help you all can offer is greatly appreciated.

I have created an Outlook Add-In via C# and Visual Studio 2013. This add-in will create a number of custom tasks, which is working. The problem I am having is searching for those custom created tasks and deleting them. I have code working to search based on a strict subject line, but nothing using "LIKE" to find partial search matches. I've also read that the find method cannot search the body, so then advanced search is better. I am trying to work with the advanced search documentation I can find, but am not sure of what the "scope" parameter would be for tasks, or even if this is the best solution.

I figured the best approach was to append all custom created tasks with a footer notated "DO NOT DELETE" and output text here that I could search to determine the task was created by my add-in and then delete it. I have also considered storing the custom task's EntryID at the time of creation, but have read that this number can change, so I am not sure this will be the best method to always find and delete the custom created tasks.

I am hoping one of you would be able to assist with a code sample of searching a user's task folder for all tasks containing a string in the body of the task. Alternatively, I can definitely work with a search of all tasks containing a string in the subject line. I am pulling my hair out over this, and I appreciate any guidance, articles, or code samples you all can provide! I either find examples in VB, or partial explanations that I am unable to put into practice.

EDIT: SOLVED

Thanks to the below response marked as an answer, I wanted to give more code detail here in case anybody needs a more detailed answer in the future. This solution does not search the body text as stated in this post title as that solution was not the best way to accomplish what I needed.

Creating the task

using Outlook = Microsoft.Office.Interop.Outlook;

Outlook.Application app = new Outlook.Application();
Outlook.TaskItem task = app.CreateItem(Outlook.OlItemType.olTaskItem) as Outlook.TaskItem;
Outlook.UserProperties taskUserProperties = null;
Outlook.UserProperty taskUserProperty = null;

try {
    taskUserProperties = task.UserProperties;
    taskUserProperty = taskUserProperties.Add("Custom Property Name", Outlook.OlUserPropertyType.olText, true, 1);
    taskUserProperty.Value = "Custom value";
    task.Save();
}
catch (Exception ex) {
    MessageBox.Show(ex.Message);
}
finally {
    if (taskUserProperty != null) Marshal.ReleaseComObject(taskUserProperty);
    if (taskUserProperties != null) Marshal.ReleaseComObject(taskUserProperties);
}

Finding it

string searchCriteria = "[Custom Property Name] = 'VALUE TO FIND'";
Outlook.Application app = new Outlook.Application();
Outlook._NameSpace ns = app.GetNamespace("MAPI");
Outlook.MAPIFolder folder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
Outlook._TaskItem taskItem = null;
Outlook.Items folderItems = null;
object resultItem = null;

try {
    folderItems = folder.Items;
    folderItems.IncludeRecurrences = true;

    if (folderItems.Count > 0) {
        resultItem = folderItems.Find(searchCriteria);

        if (resultItem != null) {
            if (resultItem is Outlook._TaskItem) {
                taskItem = resultItem as Outlook._TaskItem;

                MessageBox.Show(taskItem.Subject, "Task found!", MessageBoxButtons.OK);
            }
        }

        Marshal.ReleaseComObject(resultItem);
    }
}
catch (Exception ex) {
    MessageBox.Show(ex.Message);
}
finally {
    if (folderItems != null) Marshal.ReleaseComObject(folderItems);
}
1

There are 1 answers

3
Dmitry Streblechenko On BEST ANSWER

Why not use the user properties (TaksItem.UserPropertiers.Add)? If the user fields is added to the folder fields, you can search for that property using Items.Find/FindNext/Restrict.