In this example, we will create an Excel/PDF/Word file with some dummy data and will invoke PowerShell command Set-AIPFileLabel to classify these documents programmatically. This solution is targeted for developers, architects, COE working on automating business processes that involve MS Excel/Word/PDFs. For more interesting articles, tips & tricks please visit my blog - https://aditya07121990.blogspot.com/
Introduction
Organizations are now adopting Microsoft Azure Information Protection services to enable its operations to have all Microsoft Office documents, PDFs and email being classified based on the sensitivity of data these documents or email are holding. Meaning if you are creating an Excel report, it will be mandatory to classify the same before saving. This situation will impact all automation running across the organization.
Background
Azure Information Protection allows labelling/ protecting via PowerShell cmdlet:
- When you install the Azure Information Protection unified labelling client, PowerShell commands are automatically installed as part of the Azure Information Protection module, with cmdlet for labelling. The Azure Information Protection module enables you to manage the client by running commands for automation scripts.
Set-AIPFileLabel
: Sets or removes an Azure Information Protection label for a file, and sets or removes the protection according to the label configuration or custom permission.
Command Structure
Set-AIPFileLabel -Path <file path> -LabelId <label Id>
-Path
parameter will take the full path of the file that needs to be labelled -LabelId
parameter will take identity (ID) of the label to be applied (You need to get this label ID from your Azure Admin or Information Resource Management team in your organization)
Create Template for Excel and Word
You can create Excel and Word templates with default classification manually and store in shared location that you can use later in your code while adding a workbook or document using Office Interops.
Save a Workbook as a Template
-
Open the workbook that you want to use as a template.
-
Click File, and then click Save As.
-
In the File name box, type the name that you want to use for the template.
-
In the Save as type box, click Excel Template, or click Excel Macro-Enabled Template if the workbook contains macros that you want to make available in the template.
-
Click Save.
The template is automatically placed in the Templates folder to ensure that it will be available when you want to use it to create a new workbook.
Save a Document as a Template
-
To save a file as a template, click File > Save As.
-
Double-click Computer or, in Office 2016 programs, double-click This PC.
-
Type a name for your template in the File name box.
-
For a basic template, click the template item in the Save as type list. In Word, for example, click Word Template.
-
If your document contains macros, click Word Macro-Enabled Template.
Office automatically goes to the Custom Office Templates folder.
-
Click Save.
Using the Code for Excel
Create a new C# Windows project in Visual Studio and add the necessary references as shown below:
using System;
using System.Windows.Automation;
using Microsoft.Office.Interop.Excel;
using System.Management.Automation;
using System.IO;
You need to get System.Management.Automation DLL under C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\
The below method will create an Excel file with some dummy data and will call another method setClassification
to invoke PowerShell cmdlet and execute SetFileAPILabel
commands with required parameters.
public bool ExcelReport(string ExcelPath, string lable_id)
{
try
{
if (File.Exists(ExcelPath))
{
File.Delete(ExcelPath);
}
var excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.DisplayAlerts = false;
excelApp.Visible = false;
Object missing = Type.Missing;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook = null;
xlWorkBook = excelApp.Workbooks.Add(missing);
object misValue = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet1 =
(Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.Add
(Type.Missing);
xlWorkSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)
xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet1.Name = "Page_1";
DateTime dt = DateTime.Now;
string date = dt.ToString("dd-MsM-yyyy");
xlWorkSheet1.Activate();
xlWorkSheet1.Cells[1][1] = "TEST1";
xlWorkSheet1.Cells[1][2] = "TEST2"; xlWorkSheet1.Cells[1][3] = "TEST3";
xlWorkBook.SaveAs(ExcelPath, Type.Missing, Type.Missing,
Type.Missing, false, Type.Missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
xlWorkBook.Close(false, misValue, misValue);
excelApp.Quit();
bool result = setClassification(ExcelPath, lable_id);
if (result)
{
MessageBox.Show("Label applied successfully");
}
return true;
}
catch (Exception ex)
{
MessageBox.Show("Error:" + "\r\n" + ex.ToString());
return false;
}
}
public bool setClassification(string filename, string LabelID)
{
try
{
PowerShell.Create().AddCommand("Set-AIPFileLabel").AddParameter
("Path", filename).AddParameter("LabelId", LabelID).Invoke();
return true;
}
catch (Exception ex)
{
return false;
}
}
Using the Code for Word
Create a new C# Windows project in Visual Studio and add the necessary references as shown below:
using System;
using System.Windows.Automation;
using Microsoft.Office.Interop.Word;
using System.Management.Automation;
using System.IO;
public bool WordReport(string WordPath, string label_id, string templatePath)
{
try
{
if (File.Exists(WordPath))
{
File.Delete(WordPath);
}
Word.Application app = new Word.Application();
app.Visible = false;
object missing = System.Reflection.Missing.Value;
Word.Document doc = app.Documents.Add(templatePath);
doc.Content.SetRange(0, 0);
doc.Content.Text = "This is test document " + Environment.NewLine;
doc.SaveAs2(WordPath);
doc.Close(ref missing, ref missing, ref missing);
doc = null;
app.Quit(ref missing, ref missing, ref missing);
app = null;
bool result = setClassification(WordPath, label_id);
if (result)
{
MessageBox.Show("Label applied successfully");
}
return true;
}
catch (Exception ex)
{
MessageBox.Show("Error:" + "\r\n" + ex.ToString());
return false;
}
}
public bool setClassification(string filename, string LabelID)
{
try
{
PowerShell.Create().AddCommand("Set-AIPFileLabel").AddParameter
("Path", filename).AddParameter("LabelId", LabelID).Invoke();
return true;
}
catch (Exception ex)
{
return false;
}
}
Using the Code for PDF
Create a new C# Windows project in Visual Studio and add the necessary references as shown below:
using System;
using System.Windows.Automation;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Management.Automation;
using System.IO;
public bool createPDF(string pdfpath,string label_id)
{
System.IO.FileStream fs = new FileStream(pdfpath, FileMode.Create);
Document document = new Document(PageSize.A4, 25, 25, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
try
{
document.AddAuthor("TEST 1");
document.AddCreator("TEST 2");
document.AddKeywords("TEST 3");
document.AddSubject("TEST 4");
document.AddTitle("TEST 5");
document.Open();
document.Add(new Paragraph("Adding Azure information protection label!"));
document.Close();
writer.Close();
fs.Close();
bool result = setClassification(pdfpath, label_id);
if (result)
MessageBox.Show("Document labelled successfully");
else
MessageBox.Show("Failed to apply label");
return result;
}
catch(Exception ex)
{
MessageBox.Show("Exception occurred :" + ex.Message);
return false;
}
}
public bool setClassification(string filename, string LabelID)
{
try
{
PowerShell.Create().AddCommand("Set-AIPFileLabel").AddParameter
("Path", filename).AddParameter("LabelId", LabelID).Invoke();
return true;
}
catch (Exception ex)
{
return false;
}
}
I have not included any demo project in this article since it is very self explanatory and simple to code and implement. It would be helpful for automation developers, architect, managers involved in writing automated test scripts and Robotics Process Automation and Macro developers as well.
History
- 13th June, 2021: Initial version