Quickstart: Compute policy actions - Policy SDK (C++)

This quickstart shows you how to use the MIP Policy SDK to compute the actions that should be taken when applying or changing sensitivity labels.

Prerequisites

If you haven't already, be sure to complete the following prerequisites before continuing:

Implement ExecutionState

Before calling ComputeActions(), you need an implementation of mip::ExecutionState that provides the current and desired label state.

  1. Open the Visual Studio solution you created in the previous quickstart.

  2. Add a header file to your project and implement a simplified ExecutionState:

    #include "mip/upe/execution_state.h"
    #include "mip/upe/policy_engine.h"
    #include "mip/upe/metadata_action.h"
    #include "mip/upe/protect_by_template_action.h"
    #include <algorithm>
    #include <iostream>
    #include <memory>
    #include <string>
    #include <unordered_map>
    #include <vector>
    
    struct ExecutionStateOptions {
        std::unordered_map<std::string, std::string> metadata;
        std::shared_ptr<mip::Label> newLabel;
        std::string contentIdentifier;
        mip::AssignmentMethod assignmentMethod = mip::AssignmentMethod::STANDARD;
        bool isDowngradeJustified = false;
        std::string downgradeJustification;
        std::string contentFormat = mip::GetFileContentFormat();
        mip::ActionType supportedActions = mip::ActionType::METADATA
            | mip::ActionType::PROTECT_BY_TEMPLATE
            | mip::ActionType::JUSTIFY
            | mip::ActionType::ADD_CONTENT_FOOTER
            | mip::ActionType::ADD_CONTENT_HEADER
            | mip::ActionType::ADD_WATERMARK;
    };
    
    class ExecutionStateImpl : public mip::ExecutionState {
    public:
        ExecutionStateImpl(const ExecutionStateOptions& options) : mOptions(options) {}
    
        std::shared_ptr<mip::Label> GetNewLabel() const override {
            return mOptions.newLabel;
        }
    
        mip::AssignmentMethod GetNewLabelAssignmentMethod() const override {
            return mOptions.assignmentMethod;
        }
    
        std::shared_ptr<mip::ProtectionDescriptor> GetProtectionDescriptor() const override {
            return nullptr;
        }
    
        std::string GetContentFormat() const override {
            return mOptions.contentFormat;
        }
    
        std::string GetContentIdentifier() const override {
            return mOptions.contentIdentifier;
        }
    
        mip::ActionType GetSupportedActions() const override {
            return mOptions.supportedActions;
        }
    
        std::pair<bool, std::string> IsDowngradeJustified() const override {
            return std::make_pair(mOptions.isDowngradeJustified, mOptions.downgradeJustification);
        }
    
        std::vector<mip::MetadataEntry> GetContentMetadata(
            const std::vector<std::string>& names,
            const std::vector<std::string>& namePrefixes) const override {
            std::vector<mip::MetadataEntry> result;
            for (const auto& entry : mOptions.metadata) {
                const bool requested = (names.empty() && namePrefixes.empty())
                    || std::find(names.begin(), names.end(), entry.first) != names.end()
                    || std::any_of(namePrefixes.begin(), namePrefixes.end(),
                        [&](const std::string& prefix) { return entry.first.compare(0, prefix.size(), prefix) == 0; });
                if (requested) {
                    result.emplace_back(entry.first, entry.second);
                }
            }
            return result;
        }
    
    private:
        ExecutionStateOptions mOptions;
    };
    

Compute actions for a new label

Add logic to compute the actions required when applying a new label to unlabeled content.

In the main() function, after listing labels, add the following code:

// Replace with a valid label ID from your tenant.
string newLabelId = "<label-id>";

// Set up ExecutionState options for the new label.
ExecutionStateOptions options;
options.newLabel = engine->GetLabelById(newLabelId);
options.contentIdentifier = "MyDocument.docx";

// Create ExecutionState, policy handler, and compute actions.
std::unique_ptr<ExecutionStateImpl> state(new ExecutionStateImpl(options));
auto handler = engine->CreatePolicyHandler(false);
auto actions = handler->ComputeActions(*state);

cout << "\nActions for applying label:\n";

for (const auto& action : actions)
{
    switch (action->GetType())
    {
    case mip::ActionType::METADATA:
    {
        auto metadataAction = static_cast<mip::MetadataAction*>(action.get());
        cout << "MetadataAction:" << endl;
        for (const auto& name : metadataAction->GetMetadataToRemove())
        {
            cout << "  Remove: " << name << endl;
        }
        for (const auto& prop : metadataAction->GetMetadataToAdd())
        {
            cout << "  Add: " << prop.GetKey() << " : " << prop.GetValue() << endl;
        }
        break;
    }
    case mip::ActionType::PROTECT_BY_TEMPLATE:
    {
        auto protectAction = static_cast<mip::ProtectByTemplateAction*>(action.get());
        cout << "ProtectByTemplateAction: TemplateId = " << protectAction->GetTemplateId() << endl;
        break;
    }
    case mip::ActionType::ADD_CONTENT_FOOTER:
    {
        cout << "AddContentFooterAction" << endl;
        break;
    }
    case mip::ActionType::ADD_CONTENT_HEADER:
    {
        cout << "AddContentHeaderAction" << endl;
        break;
    }
    case mip::ActionType::ADD_WATERMARK:
    {
        cout << "AddWatermarkAction" << endl;
        break;
    }
    case mip::ActionType::JUSTIFY:
    {
        cout << "JustifyAction" << endl;
        break;
    }
    default:
        cout << "Other action type: " << static_cast<int>(action->GetType()) << endl;
        break;
    }
}

Handle a label downgrade scenario

When changing from a higher sensitivity label to a lower one, the policy may require justification. Replace both label-ID placeholders with distinct labels from your tenant. In production, read the complete existing metadata from the content and obtain justification from the user; the following code uses simplified metadata and a fixed justification for demonstration.

// Simulate existing metadata from a "Confidential" label.
ExecutionStateOptions downgradeOptions;
downgradeOptions.newLabel = engine->GetLabelById("<lower-sensitivity-label-id>");
downgradeOptions.contentIdentifier = "MyDocument.docx";
downgradeOptions.isDowngradeJustified = true;
downgradeOptions.downgradeJustification = "Manager approved declassification";

// Populate existing label metadata (from the previously applied label).
downgradeOptions.metadata["MSIP_Label_<previous-label-id>_Enabled"] = "true";
downgradeOptions.metadata["MSIP_Label_<previous-label-id>_Method"] = "Standard";

std::unique_ptr<ExecutionStateImpl> downgradeState(new ExecutionStateImpl(downgradeOptions));
auto downgradeHandler = engine->CreatePolicyHandler(false);
auto downgradeActions = downgradeHandler->ComputeActions(*downgradeState);

cout << "\nActions for label downgrade:\n";
for (const auto& action : downgradeActions)
{
    cout << "  Action type: " << static_cast<int>(action->GetType()) << endl;
}

Build and test the application

Build and test your client application.

  1. Use Ctrl+Shift+B (Build Solution) to build the application. If you have no build errors, use F5 (Start debugging) to run.

  2. On first run, the application prompts for authentication. Provide credentials for a user account in your tenant.

  3. The output should display the computed actions for the specified label, similar to:

    Actions for applying label:
    MetadataAction:
      Add: MSIP_Label_<label-id>_Enabled : true
      Add: MSIP_Label_<label-id>_SetDate : 2026-03-11T10:00:00-0800
      Add: MSIP_Label_<label-id>_Method : Standard
      Add: MSIP_Label_<label-id>_Name : Confidential
      Add: MSIP_Label_<label-id>_SiteId : <site-id>
      Add: MSIP_Label_<label-id>_ActionId : <action-id>
      Add: MSIP_Label_<label-id>_ContentBits : 3
    AddContentFooterAction
    AddContentHeaderAction
    

Next steps

This quickstart only displays actions; it doesn't label, mark, or protect the document. A complete application must implement every action it advertises through GetSupportedActions(). Handle justification before committing changes, and call NotifyCommittedActions() only after the actions have been applied and the content successfully committed.