Search code examples
c#microsoft-information-protectionmip-sdk

How can we encrypt a PDF file using MIP SDK?


Basically our requirement is to download existing pdf document (having MIP encryption), then decrypt the PDF file, modify content in PDF and then encrypt back. I am using File SDK. Till modifying content I am able to perform, now I want to encrypt back the file. While setting the Label (having encryption enabled from protection.office.com), getting error as "TemplateNotFoundException: Unrecognized template ID.,". Please suggest.

var result = Task.Run(async () => await handler.CommitAsync(options.OutputName)).Result;

enter image description here


Solution

  • It would seem that the RMS protection template associated with the label has been removed or set to archived. You can either delete and re-create that label, or you'll have to use the AIP service cmdlets to investigate if one of your templates has been set to archived, or deleted.

    You can use Get-AIPServiceTemplate to list the templates. The name of the template should match your label.

    If the template is set to archived, use Set-AIPSerivceTemplate to set the state back to published. If the template doesn't exist, you can delete the label and start over (though it's odd the template would be deleted), or you can use PowerShell to update the protection settings.

    This doc describes how to manage labels via PowerShell. You would do something like:

    get-label | where {$_.displayname -eq "TOP SECRET"} | fl displayname,labelactions
    

    Which would output:

    DisplayName  : TOP SECRET
    LabelActions : {{"Type":"encrypt","SubType":null,"Settings":[{"Key":"disabled","Value":"false"},{"Key":"protectiontype"
                   ,"Value":"template"},{"Key":"templateid","Value":"0e7fea72-7bba-4438-a070-95c292cd6f8c"},{"Key":"templat
                   earchived","Value":"True"},{"Key":"linkedtemplateid","Value":"0e7fea72-7bba-4438-a070-95c292cd6f8c"},{"K
                   ey":"contentexpiredondateindaysornever","Value":"Never"},{"Key":"offlineaccessdays","Value":"-1"},{"Key"
                   :"rightsdefinitions","Value":"[{\"Identity\":\"miltor.onmicrosoft.com\",\"Rights\":\"VIEW,VIEWRIGHTSDATA
                   ,DOCEDIT,EDIT,PRINT,EXTRACT,REPLY,REPLYALL,FORWARD,OBJMODEL\"},{\"Identity\":\"microsoft.com\",\"Rights\
                   ":\"VIEW,VIEWRIGHTSDATA,OBJMODEL\"},{\"Identity\":\"[email protected]\",\"Rights\":\"PROJECT\"}]"}]}, {"Ty
                   pe":"applycontentmarking","SubType":"header","Settings":[{"Key":"fontsize","Value":"10"},{"Key":"placeme
                   nt","Value":"Header"},{"Key":"text","Value":"Property of Contoso"},{"Key":"fontcolor","Value":"#000000"}
                   ,{"Key":"margin","Value":"5"},{"Key":"alignment","Value":"Left"},{"Key":"disabled","Value":"false"}]}, {
                   "Type":"applycontentmarking","SubType":"footer","Settings":[{"Key":"fontsize","Value":"10"},{"Key":"plac
                   ement","Value":"Footer"},{"Key":"text","Value":"Property of Contoso"},{"Key":"fontcolor","Value":"#00000
                   0"},{"Key":"margin","Value":"5"},{"Key":"alignment","Value":"Left"},{"Key":"disabled","Value":"false"}]}
                   , {"Type":"applywatermarking","SubType":null,"Settings":[{"Key":"fontsize","Value":"10"},{"Key":"layout"
                   ,"Value":"Diagonal"},{"Key":"fontcolor","Value":"#000000"},{"Key":"disabled","Value":"false"},{"Key":"te
                   xt","Value":"Property of Contoso"}]}}
    

    In that JSON, find linkedtemplateid: {"Key":"linkedtemplateid","Value":"0e7fea72-7bba-4438-a070-95c292cd6f8c"}

    And that's the template ID that should exist in RMS to provide the protection action for the label. If it doesn't exist, use Add-AIPServiceTemplate to create a new template and update the label by Set-Label and providing the EncryptionProtectionType and EncryptionRightsDefinitions.