Working with MapiTask in PST

An Outlook task describes a piece of work with a start and a due date, a progress value, effort figures and the people involved in assigning it. Aspose.Email for C++ models it with the MapiTask class; inside a PST file tasks live in a folder with the tasks role.

The sample in this article uses these headers and namespaces:

#include <Mapi/MapiMessage.h>
#include <Mapi/MapiTask.h>
#include <Mapi/MapiTaskUsers.h>
#include <Mapi/MapiTaskHistory.h>
#include <Mapi/MapiTaskOwnership.h>
#include <Mapi/MapiItemType.h>
#include <Storage/Pst/PersonalStorage.h>
#include <Storage/Pst/FolderInfo.h>
#include <Storage/Pst/MessageInfo.h>
#include <Storage/Pst/FileFormatVersion.h>
#include <Storage/Pst/StandardIpmFolder.h>
#include <system/convert.h>

using namespace System;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::Storage::Pst;

Adding MapiTask to PST

The MapiTask constructor takes the subject, the body, the start date and the due date. The remaining details are properties:

The task is then added to a folder created with StandardIpmFolder::Tasks and read back from it.

auto task = MakeObject<MapiTask>(u"Prepare the release notes", u"Collect the changes merged since the last release.",
    DateTime(2025, 5, 5), DateTime(2025, 5, 9));
task->set_PercentComplete(20);
task->set_EstimatedEffort(480);
task->set_ActualEffort(90);
task->set_History(MapiTaskHistory::Assigned);
task->set_LastUpdate(DateTime(2025, 5, 6, 16, 30, 0));
task->get_Users()->set_Owner(u"Darius");
task->get_Users()->set_LastAssigner(u"Harkness");
task->get_Users()->set_LastDelegate(u"Harkness");
task->get_Users()->set_Ownership(MapiTaskOwnership::AssignersCopy);

auto pst = PersonalStorage::Create(u"Tasks.pst", FileFormatVersion::Unicode);
auto taskFolder = pst->CreatePredefinedFolder(u"Tasks", StandardIpmFolder::Tasks);
taskFolder->AddMapiMessageItem(task);

for (auto messageInfo : taskFolder->EnumerateMessages())
{
    auto message = pst->ExtractMessage(messageInfo);
    if (message->get_SupportedType() != MapiItemType::Task)
    {
        continue;
    }

    auto stored = DynamicCast<MapiTask>(message->ToMapiMessageItem());
    Console::WriteLine(stored->get_Subject() + u": " + Convert::ToString(stored->get_PercentComplete()) +
        u"% done, owner " + stored->get_Users()->get_Owner());
}

pst->Dispose();