Manage Sensitivity Labels in PowerPoint Presentations in .NET

Overview

Microsoft Purview sensitivity labels help organizations classify and govern documents. During automated presentation processing, an application may need to preserve an existing label, apply a label selected by a policy, update its state, or migrate label metadata written by an older Microsoft Information Protection (MIP) workflow.

Aspose.Slides exposes modern sensitivity label metadata through Presentation.SensitivityLabels. This property returns an ISensitivityLabelCollection that can be inspected and modified before the presentation is saved as PPTX.

Understand Sensitivity Label Properties

Each ISensitivityLabel contains the following metadata:

Property Purpose
ISensitivityLabel.Id Identifies the sensitivity label in the Purview policy.
ISensitivityLabel.SiteId Identifies the site associated with the label policy.
ISensitivityLabel.IsEnabled Indicates whether the label is enabled.
ISensitivityLabel.IsRemoved Indicates that the label has been removed. Set this property to true when the removal state must be retained in the metadata.
ISensitivityLabel.AssignmentMethodType Specifies whether the label was applied automatically or through a user decision.
ISensitivityLabel.ContentMarkTypes Lists the content marking types associated with the label.

The SensitivityLabelAssignmentType enumeration describes how a label was assigned:

The SensitivityLabelContentType enumeration identifies the marking associated with a label:

Value Meaning
SensitivityLabelContentType.None The label was applied by default or automatically.
SensitivityLabelContentType.Header Header content marking is associated with the label.
SensitivityLabelContentType.Footer Footer content marking is associated with the label.
SensitivityLabelContentType.Watermark Watermark content marking is associated with the label.
SensitivityLabelContentType.Encryption Encryption protection is associated with the label.

Multiple marking types can be associated with one label.

List Existing Sensitivity Labels

Read the modern label collection from Presentation.SensitivityLabels and enumerate it. The following example lists every property and content marking stored for each label:

using System;
using Aspose.Slides;

using var presentation = new Presentation("presentation.pptx");
var sensitivityLabels = presentation.SensitivityLabels;

foreach (var sensitivityLabel in sensitivityLabels)
{
    Console.WriteLine("Label ID: " + sensitivityLabel.Id);
    Console.WriteLine("Site ID: " + sensitivityLabel.SiteId);
    Console.WriteLine("Enabled: " + sensitivityLabel.IsEnabled);
    Console.WriteLine("Removed: " + sensitivityLabel.IsRemoved);
    Console.WriteLine("Assignment method: " + sensitivityLabel.AssignmentMethodType);

    foreach (var contentMarkType in sensitivityLabel.ContentMarkTypes)
    {
        Console.WriteLine("Content marking: " + contentMarkType);
    }
}

Add a Sensitivity Label with Content Marking

Use ISensitivityLabelCollection.Add with the label identifier, site identifier, enabled state, and assignment method. After the method returns the new ISensitivityLabel, add the required marking values through ISensitivityLabel.ContentMarkTypes.

The following example adds a manually selected label associated with footer and watermark markings, and then saves the result as PPTX:

using System;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation("presentation.pptx");
var sensitivityLabels = presentation.SensitivityLabels;

var labelIdentifier = "{11111111-2222-3333-4444-555555555555}";
var siteIdentifier = Guid.Parse("{aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee}");
var isEnabled = true;
var assignmentMethod = SensitivityLabelAssignmentType.Privileged;

var sensitivityLabel = sensitivityLabels.Add(
    labelIdentifier,
    siteIdentifier,
    isEnabled,
    assignmentMethod);

sensitivityLabel.ContentMarkTypes.Add(SensitivityLabelContentType.Footer);
sensitivityLabel.ContentMarkTypes.Add(SensitivityLabelContentType.Watermark);

presentation.Save("presentation_with_label.pptx", SaveFormat.Pptx);

Update a Sensitivity Label

The ISensitivityLabel properties are read/write, except that the collection returned by ISensitivityLabel.ContentMarkTypes is modified through its list operations. After locating the required label, you can update its identifier, site identifier, enabled state, assignment method, removal state, and content marking types. Save the presentation to persist the changes.

The following example updates the enabled state and assignment method of the first label:

using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation("presentation.pptx");
var sensitivityLabels = presentation.SensitivityLabels;

if (sensitivityLabels.Count > 0)
{
    var sensitivityLabel = sensitivityLabels[0];
    sensitivityLabel.IsEnabled = true;
    sensitivityLabel.AssignmentMethodType = SensitivityLabelAssignmentType.Privileged;
}

presentation.Save("presentation_with_updated_label.pptx", SaveFormat.Pptx);

Mark a Sensitivity Label as Removed

To preserve the fact that a label was removed, find the label and set ISensitivityLabel.IsRemoved to true. This retains the label entry while recording its removed state. If you instead need to delete an entry from the modern collection, use ISensitivityLabelCollection.RemoveAt; use ISensitivityLabelCollection.Clear to delete every entry.

The following example marks a specific label as removed and saves the updated presentation:

using System;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation("presentation.pptx");
var sensitivityLabels = presentation.SensitivityLabels;
var targetLabelIdentifier = "{11111111-2222-3333-4444-555555555555}";

foreach (var sensitivityLabel in sensitivityLabels)
{
    var isTargetLabel = string.Equals(
        sensitivityLabel.Id,
        targetLabelIdentifier,
        StringComparison.OrdinalIgnoreCase);

    if (isTargetLabel)
    {
        sensitivityLabel.IsRemoved = true;
        break;
    }
}

presentation.Save("presentation_with_removed_label.pptx", SaveFormat.Pptx);

Read and Migrate Legacy MIP Sensitivity Labels

Older MIP-based workflows can store sensitivity label metadata in custom document properties instead of the modern label collection. Read that metadata with IDocumentProperties.GetSensitivityLabels. The method parses the legacy custom properties and returns an array of ISensitivityLabel objects.

To migrate the metadata, add each returned label to the modern ISensitivityLabelCollection through ISensitivityLabelCollection.Add. Because adding a duplicate label identifier raises an exception, the example checks the destination collection before copying each label. You can add further validation to confirm that each legacy label still exists in the current Purview policy.

using System;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation("presentation_with_legacy_labels.pptx");
var legacySensitivityLabels = presentation.DocumentProperties.GetSensitivityLabels();
var modernSensitivityLabels = presentation.SensitivityLabels;

foreach (var legacySensitivityLabel in legacySensitivityLabels)
{
    var labelAlreadyExists = false;

    foreach (var modernSensitivityLabel in modernSensitivityLabels)
    {
        labelAlreadyExists = string.Equals(
            modernSensitivityLabel.Id,
            legacySensitivityLabel.Id,
            StringComparison.OrdinalIgnoreCase);

        if (labelAlreadyExists)
        {
            break;
        }
    }

    if (!labelAlreadyExists)
    {
        modernSensitivityLabels.Add(legacySensitivityLabel);
    }
}

presentation.Save("presentation_with_modern_labels.pptx", SaveFormat.Pptx);

The migration copies the parsed label objects into the modern collection. It does not require clearing all custom document properties, so unrelated document metadata remains intact. Use IPresentation.Save with SaveFormat.Pptx to write the modern label metadata to a PPTX file.

FAQ

Does adding a content marking type create a visible header, footer, or watermark on slides?

No. Values added through ISensitivityLabel.ContentMarkTypes describe the markings associated with the sensitivity label. They do not create visible text or shapes in the presentation. Add the corresponding slide content separately if your workflow must render those markings.

What is the difference between marking a label as removed and deleting it from the collection?

Setting ISensitivityLabel.IsRemoved to true keeps the label entry and records its removed state. Calling ISensitivityLabelCollection.RemoveAt deletes the entry from the modern collection. Choose the operation that matches your organization’s metadata retention requirements.

Can a presentation contain both legacy MIP metadata and modern sensitivity labels?

Yes. Legacy labels can remain in custom document properties while modern labels are available through Presentation.SensitivityLabels. Use IDocumentProperties.GetSensitivityLabels to read the legacy metadata and migrate only the valid labels that are not already present in the modern collection.

What happens when a label with the same identifier is added more than once?

ISensitivityLabelCollection.Add throws an ArgumentException when the collection already contains a label with the same identifier. Check existing ISensitivityLabel.Id values before adding or migrating labels.

Which output format should be used to preserve updated sensitivity labels?

Save the presentation as PPTX by calling IPresentation.Save with SaveFormat.Pptx, as shown in the examples above.