Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / PowerShell
Tip/Trick

Implementing Azure Information Protection in MS Office documents and PDFs programmatically

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
20 Jun 2021CPOL3 min read 11.9K   2  
How to set classification label to an Excel/Word/PDF file at the time of its creation during runtime programmatically
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/

Image 1

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

  1. Open the workbook that you want to use as a template.

  2. Click File, and then click Save As.

  3. In the File name box, type the name that you want to use for the template.

  4. 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.

  5. 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

  1. To save a file as a template, click File > Save As.

  2. Double-click Computer or, in Office 2016 programs, double-click This PC.

  3. Type a name for your template in the File name box.

  4. For a basic template, click the template item in the Save as type list. In Word, for example, click Word Template.

  5. If your document contains macros, click Word Macro-Enabled Template.

    Office automatically goes to the Custom Office Templates folder.

  6. Click Save.

Using the Code for Excel

Create a new C# Windows project in Visual Studio and add the necessary references as shown below:

C#
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.

C#
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:

C#
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:

C#
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Employed (other)
India India
I am currently employed with a top global bank as Technical manager and part of Robotics Process Automation maintenance team with over 9 years of experience in digital automation space and delivered projects across industries such as Telecom, Insurance, Finance, Retail, Banking etc.

Comments and Discussions

 
-- There are no messages in this forum --