Manage Sensitivity Labels in PowerPoint Presentations in PHP
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 for PHP via Java exposes modern sensitivity label metadata through Presentation::getSensitivityLabels. This method returns a SensitivityLabelCollection that can be inspected and modified before the presentation is saved as PPTX.
Note
Sensitivity label identifiers and policy information are defined by your Microsoft Purview configuration. Validate label availability and policy requirements in your environment before adding or migrating metadata. The SensitivityLabel::getContentMarkTypes values describe the content markings associated with a label; they do not by themselves add visible text or shapes to slides.Understand Sensitivity Label Properties
Each SensitivityLabel contains the following metadata:
| Methods | Purpose |
|---|---|
| SensitivityLabel::getId and SensitivityLabel::setId | Get or set the sensitivity label identifier in the Purview policy. |
| SensitivityLabel::getSiteId and SensitivityLabel::setSiteId | Get or set the site associated with the label policy. |
| SensitivityLabel::isEnabled and SensitivityLabel::setEnabled | Get or set whether the label is enabled. |
| SensitivityLabel::isRemoved and SensitivityLabel::setRemoved | Get or set whether the label has been removed. Set the value to true when the removal state must be retained in the metadata. |
| SensitivityLabel::getAssignmentMethodType and SensitivityLabel::setAssignmentMethodType | Get or set whether the label was applied automatically or through a user decision. |
| SensitivityLabel::getContentMarkTypes | Get the content marking types associated with the label. |
The SensitivityLabelAssignmentType class defines how a label was assigned:
- SensitivityLabelAssignmentType::Standard represents a default or automatically applied label.
- SensitivityLabelAssignmentType::Privileged represents a label applied through a user decision, including manually applied, recommended, and mandatory labels.
The SensitivityLabelContentType class defines 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::getSensitivityLabels and enumerate it. The following example lists every property and content marking stored for each label:
$presentation = new Presentation("presentation.pptx");
try {
$sensitivityLabels = $presentation->getSensitivityLabels();
$sensitivityLabelCount = java_values($sensitivityLabels->getCount());
for ($labelIndex = 0; $labelIndex < $sensitivityLabelCount; $labelIndex++) {
$sensitivityLabel = $sensitivityLabels->get_Item($labelIndex);
echo "Label ID: " . java_values($sensitivityLabel->getId()) . PHP_EOL;
echo "Site ID: " . java_values($sensitivityLabel->getSiteId()->toString()) . PHP_EOL;
echo "Enabled: " . (java_values($sensitivityLabel->isEnabled()) ? "true" : "false") . PHP_EOL;
echo "Removed: " . (java_values($sensitivityLabel->isRemoved()) ? "true" : "false") . PHP_EOL;
echo "Assignment method: " . java_values($sensitivityLabel->getAssignmentMethodType()) . PHP_EOL;
$contentMarkIterator = $sensitivityLabel->getContentMarkTypes()->iterator();
while (java_values($contentMarkIterator->hasNext())) {
$contentMarkType = java_values($contentMarkIterator->next());
echo "Content marking: " . $contentMarkType . PHP_EOL;
}
}
} finally {
$presentation->dispose();
}
Add a Sensitivity Label with Content Marking
Use SensitivityLabelCollection::add with the label identifier, site identifier, enabled state, and assignment method. After the method returns the new SensitivityLabel, add the required marking values through the list returned by SensitivityLabel::getContentMarkTypes.
The following example adds a manually selected label associated with footer and watermark markings, and then saves the result as PPTX:
$presentation = new Presentation("presentation.pptx");
try {
$sensitivityLabels = $presentation->getSensitivityLabels();
$labelIdentifier = "{11111111-2222-3333-4444-555555555555}";
$UUID = new JavaClass("java.util.UUID");
$siteIdentifier = $UUID->fromString("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee");
$isEnabled = true;
$assignmentMethod = SensitivityLabelAssignmentType::Privileged;
$sensitivityLabel = $sensitivityLabels->add(
$labelIdentifier,
$siteIdentifier,
$isEnabled,
$assignmentMethod
);
$contentMarkTypes = $sensitivityLabel->getContentMarkTypes();
$contentMarkTypes->addItem(SensitivityLabelContentType::Footer);
$contentMarkTypes->addItem(SensitivityLabelContentType::Watermark);
$presentation->save("presentation_with_label.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Update a Sensitivity Label
The SensitivityLabel values are read/write, except that the list returned by SensitivityLabel::getContentMarkTypes 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:
$presentation = new Presentation("presentation.pptx");
try {
$sensitivityLabels = $presentation->getSensitivityLabels();
$sensitivityLabelCount = java_values($sensitivityLabels->getCount());
if ($sensitivityLabelCount > 0) {
$sensitivityLabel = $sensitivityLabels->get_Item(0);
$sensitivityLabel->setEnabled(true);
$sensitivityLabel->setAssignmentMethodType(SensitivityLabelAssignmentType::Privileged);
}
$presentation->save("presentation_with_updated_label.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Mark a Sensitivity Label as Removed
To preserve the fact that a label was removed, find the label and call SensitivityLabel::setRemoved with true. This retains the label entry while recording its removed state. If you instead need to delete an entry from the modern collection, use SensitivityLabelCollection::removeAt; use SensitivityLabelCollection::clear to delete every entry.
The following example marks a specific label as removed and saves the updated presentation:
$presentation = new Presentation("presentation.pptx");
try {
$sensitivityLabels = $presentation->getSensitivityLabels();
$targetLabelIdentifier = "{11111111-2222-3333-4444-555555555555}";
$sensitivityLabelCount = java_values($sensitivityLabels->getCount());
for ($labelIndex = 0; $labelIndex < $sensitivityLabelCount; $labelIndex++) {
$sensitivityLabel = $sensitivityLabels->get_Item($labelIndex);
$labelIdentifier = java_values($sensitivityLabel->getId());
$isTargetLabel = strcasecmp($labelIdentifier, $targetLabelIdentifier) === 0;
if ($isTargetLabel) {
$sensitivityLabel->setRemoved(true);
break;
}
}
$presentation->save("presentation_with_removed_label.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
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 DocumentProperties::getSensitivityLabels. The method parses the legacy custom properties and returns a Java array of SensitivityLabel objects.
To migrate the metadata, add each returned label to the modern SensitivityLabelCollection through SensitivityLabelCollection::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.
$presentation = new Presentation("presentation_with_legacy_labels.pptx");
try {
$legacySensitivityLabels = $presentation->getDocumentProperties()->getSensitivityLabels();
$modernSensitivityLabels = $presentation->getSensitivityLabels();
$Array = new JavaClass("java.lang.reflect.Array");
$legacyLabelCount = java_values($Array->getLength($legacySensitivityLabels));
for ($legacyLabelIndex = 0; $legacyLabelIndex < $legacyLabelCount; $legacyLabelIndex++) {
$legacySensitivityLabel = $legacySensitivityLabels[$legacyLabelIndex];
$legacyLabelIdentifier = java_values($legacySensitivityLabel->getId());
$labelAlreadyExists = false;
$modernLabelCount = java_values($modernSensitivityLabels->getCount());
for ($modernLabelIndex = 0; $modernLabelIndex < $modernLabelCount; $modernLabelIndex++) {
$modernSensitivityLabel = $modernSensitivityLabels->get_Item($modernLabelIndex);
$modernLabelIdentifier = java_values($modernSensitivityLabel->getId());
$labelAlreadyExists = strcasecmp(
$modernLabelIdentifier,
$legacyLabelIdentifier
) === 0;
if ($labelAlreadyExists) {
break;
}
}
if (!$labelAlreadyExists) {
$modernSensitivityLabels->add($legacySensitivityLabel);
}
}
$presentation->save("presentation_with_modern_labels.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
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 Presentation::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 the list returned by SensitivityLabel::getContentMarkTypes 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?
Calling SensitivityLabel::setRemoved with true keeps the label entry and records its removed state. Calling SensitivityLabelCollection::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::getSensitivityLabels. Use DocumentProperties::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?
SensitivityLabelCollection::add raises an exception when the collection already contains a label with the same identifier. Check existing values returned by SensitivityLabel::getId before adding or migrating labels.
Which output format should be used to preserve updated sensitivity labels?
Save the presentation as PPTX by calling Presentation::save with SaveFormat::Pptx, as shown in the examples above.