I'm using the Microsoft Information Protection (MIP) SDK in C# to apply sensitivity labels to files. When I apply certain labels, the code works without any issues. However, when I try to apply a specific label, I encounter an error, and the label is not applied.
Here's the code snippet where the error occurs:
public static void ChangeFileLabel(string filePath, string labelId)
{
using (var fileEngine = GetFileEngine())
{
using (var fileHandler = Task.Run(async () => await fileEngine.CreateFileHandlerAsync(filePath, filePath, true)).Result)
{
LabelingOptions labelingOptions = new LabelingOptions()
{
AssignmentMethod = AssignmentMethod.Standard,
IsDowngradeJustified = true,
JustificationMessage = "test"
};
fileHandler.SetLabel(label, labelingOptions, new ProtectionSettings());
using (var memoryStream = new MemoryStream())
{
Task.Run(async () => await fileHandler.CommitAsync(memoryStream)).GetAwaiter().GetResult();
memoryStream.Position = 0;
using (var outputFileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
memoryStream.CopyTo(outputFileStream);
}
}
}
}
}
I'm getting the following compilation errors:
Microsoft.InformationProtection.Exceptions.AdhocProtectionRequiredException: 'Label requires ad-hoc protection, but protection has not yet been set. Call FileHandler::SetProtection with ad-hoc protection settings before calling FileHandler::SetLabel.'
Additional Information:
- The label IDs are from labels created under different admin accounts in the Microsoft Defender portal.
- Both labels are sensitivity labels, but perhaps there's a difference in their configuration.
- I'm using application authentication.
How can I modify my code to handle this problem? Any guidance would be greatly appreciated. Thank you!