C# MS Office Interop apply Azure Information Protection (AIP)

919 Views Asked by At

I need to create PowerPoints but when I’m saving the file (SaveAs method), there is Azure classification add-in which requires manual click (public/internal/confidential) to finish saving the ppt file.

using Microsoft.Office.Interop.PowerPoint;
var pptApplication = new Application();
var pptPresentation = pptApplication.Presentations.Add();
pptPresentation.SaveAs(@"C:\temp\test.pptx", PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTrue);
pptPresentation.Close();
pptApplication.Quit();

I found online PowerShell method of applying labels but even if it works, it’s done on already existing(saved) file.

Then another find is https://stackoverflow.com/a/57413086/11305428 but I can’t figure out how to apply the label MSIP_Label__Enabled=True

I have the GUID of tags used in my org

strMSIPClassConfidential = "MSIP_Label_-GUID-_Enabled=true;

but haven‘t found how I would apply it to metadata with provided interop. Anyone knows?

Using PowerPoint interop because I haven't found any free libraries for this.

2

There are 2 best solutions below

1
On BEST ANSWER

Got it working. If anyone is up to commenting of what the code does feel free to edit.

void ApplyAIPLabel(Presentation pptPresentation) {
  var customDocumentProperties = pptPresentation.CustomDocumentProperties;
  var typeDocCustomProps = customDocumentProperties.GetType();
  var propertyName = "MSIP_Label_<GUID>_Enabled";
  var propertyValue = "True";
  object[] oArgs = { propertyName, false, MsoDocProperties.msoPropertyTypeString, propertyValue };
  typeDocCustomProps.InvokeMember("Add", BindingFlags.Default | BindingFlags.InvokeMethod, null, customDocumentProperties, oArgs);
}
2
On

Further Information for those finding this entry:

There are number of Custom Properties added to Office files when saving manually with AIP enabled when you choose the appropriate Lable.

They are named (the Custom Property) in the format "MSIP_Label__" with an associated value for the Property.

To work out which Labels are available (ie: configured for your Organisation) you can find information online on various on PowerShell commands to interrogate and list them.

However if you are just interested in only a few label options (e.g Confidential, Unofficial, Official) for your programming task then you can first create manually different sample office files and save them with different Sensitivity Labels.

Then open each one in turn in e.g a Word API_BlankDocument_CofidentialMedialAIPLable.docx (note: older format .doc doesn't work I beleive) and click File --> Info, Then click on the Properties v Header, and choose Advanced Properties from the only option that pops up.

An Dialog will open, now click on the "Custom" Tab, and you will see (partially visible) the various MSIP_Label_??? that Word applies when saving the document.

Screenshot of Word's Info page

You can then extract then Properties Name and Value by clicking on each Custom Property in turn and copy/pasting from "Name" and "Value" fields. Note that all values are of Type "Text" (despite one being an ISO Dates and one Numerical).

You can the use the Office object (docobj, pptobj, xlsobj) CustomProperties property and its .Add method to create the labels you need on the fly (** this logic assumes the object does not already have those properties already **).

In VBA this is the routine I created which is passed a docobj too have the required Properties added:

Sub Add_AzureInformationProtection(docobj As Object)

Const msoPropertyTypeString = 4
    
docobj.CustomDocumentProperties.Add Name:="MSIP_Label_1c33bed7-778e-416d-a71d-8913a1a68f5f_Enabled", LinkToContent:=False, TYPE:=msoPropertyTypeString, Value:="true"
docobj.CustomDocumentProperties.Add Name:="MSIP_Label_1c33bed7-778e-416d-a71d-8913a1a68f5f_Method", LinkToContent:=False, TYPE:=msoPropertyTypeString, Value:="Privileged"
docobj.CustomDocumentProperties.Add Name:="MSIP_Label_1c33bed7-778e-416d-a71d-8913a1a68f5f_Name", LinkToContent:=False, TYPE:=msoPropertyTypeString, Value:="-OFFICIAL Sensitive - Medical in confidence"
docobj.CustomDocumentProperties.Add Name:="MSIP_Label_1c33bed7-778e-416d-a71d-8913a1a68f5f_SetDate", LinkToContent:=False, TYPE:=msoPropertyTypeString, Value:=Format(Now(), "yyyy-mm-ddThh:MM:ssZ")
docobj.CustomDocumentProperties.Add Name:="MSIP_Label_1c33bed7-778e-416d-a71d-8913a1a68f5f_SiteId", LinkToContent:=False, TYPE:=msoPropertyTypeString, Value:="bda528f7-fca9-432f-bc98-bd7e90d40906"
docobj.CustomDocumentProperties.Add Name:="MSIP_Label_1c33bed7-778e-416d-a71d-8913a1a68f5f_ActionId", LinkToContent:=False, TYPE:=msoPropertyTypeString, Value:="a8cfa876-8f13-42a4-b1f3-5f5795a32c0c"
docobj.CustomDocumentProperties.Add Name:="MSIP_Label_1c33bed7-778e-416d-a71d-8913a1a68f5f_ContentBits", LinkToContent:=False, TYPE:=msoPropertyTypeString, Value:="1"

End Sub

Information on these labels (some of which may be superseeded in future AIP versions) I found here:

https://learn.microsoft.com/en-us/information-protection/develop/concept-mip-metadata