To interact with sensitivity labels programmatically in C#, you need to use the Microsoft Information Protection (MIP) SDK. The MIP SDK provides the necessary APIs to set, get, and manage sensitivity labels on various file types, including Word documents.
Here's a general outline of how to work with sensitivity labels using the MIP SDK in C#:
Prerequisites:
MIP SDK: Install the Microsoft Information Protection SDK NuGet package in your C# project.
Azure AD Application Registration: Register a client application in Azure Active Directory and grant it the necessary permissions for Microsoft Information Protection Sync Service (e.g., UnifiedPolicy.Tenant.Read) and Azure Rights Management Services (e.g., Content.SuperUser, Content.Writer).
MIP Engine Initialization: Initialize the MIP engine in your application, which involves providing credentials and configuring the engine for label operations.
Setting a Sensitivity Label:
Create an IFileHandler: Use the MIP engine to create an IFileHandler object for the target Word document (stream or file path).
Get the Label: Retrieve the desired SensitivityLabel object using its ID or name from the MIP engine.
Set the Label: Use the SetLabel method of the IFileHandler, providing the SensitivityLabel and LabelingOptions (e.g., AssignmentMethod).
Commit Changes: Call the CommitAsync method on the IFileHandler to apply the label and save the changes to the document.
Getting a Sensitivity Label:
Create an IFileHandler: Obtain an IFileHandler for the Word document.
Get the Label: Use the GetLabel method of the IFileHandler to retrieve the currently applied SensitivityLabel information. This will provide details like the label ID, name, and assignment method.
Important Considerations:
Encryption:
Sensitivity labels often involve encryption. The MIP SDK handles this automatically when applying labels that are configured with encryption.
User Context:
The MIP SDK operates within a user context, so the permissions and labels available will depend on the authenticated user.
Open XML SDK Limitations:
While the Open XML SDK can manipulate the content and structure of Word documents, it does not directly manage or understand sensitivity labels or the associated protection mechanisms. The MIP SDK is specifically designed for this purpose.