Handling Azure Data Classification in MS Excel/Word/PDFs
Organizations are now adopting Microsoft Azure Information Protection services to enable its operations to have all MS 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.
Azure Information Protection allows labeling/ protecting via PowerShell cmdlet
- When you install the Azure Information Protection unified labeling client, PowerShell commands are automatically installed as part of the Azure Information Protection module, with cmdlet for labeling. 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 remove 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 labeled-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 Interop.
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; } } //The below method is a PowerShell execution script to apply data classification label based on the Label ID provided 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; //The below method will create dummy word file and apply data classification label to it as per the provided label ID 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; //Adding template with default classification we will later elevate this classification based on the label Id provided in the parameter Word.Document doc = app.Documents.Add(templatePath); //adding text to document 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; } } //The below method is a PowerShell execution script to apply data classification label based on the Label ID provided 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 labeled successfully"); else MessageBox.Show("Failed to apply label"); return result; } catch(Exception ex) { MessageBox.Show("Exception occured :" + 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; } }
Learn Pega
ReplyDeletePega Training Hyderabad
your valuable information and time. Please keep updating.
ReplyDeletePega BA Online Training
Pega BA Online Course
Thank you for sharing an informative article on handling Azure data classification in MS Excel, Word, and PDFs. It provided valuable insights into managing and organizing data effectively. Additionally, if you're looking for web data mining RPA solutions in India , I highly recommend checking out RPA Based Solutions. Their expertise in RPA can assist in extracting valuable data from the web efficiently.
ReplyDelete