Search code examples
controlsms-worddocumentopenxml

OpenXML: Allow editing of Content Controls in locked Word document


I want to create a Word document that works as a template, where all the document is locked from editing except the Content Controls (<sdt/> elements) in the document that the user can edit.

What I've seen is that if I lock the document edition (right now I'm using the _markAsFinal property) there's no way to unlock a single Content Control.

Am I missing something? Or is this by design?


Solution

  • In your settings.xml file, you'll want under <w:settings/> an element like this:

    <w:documentProtection w:edit="forms" w:enforcement="1" w:cryptProviderType="rsaFull"
        w:cryptAlgorithmClass="hash" w:cryptAlgorithmType="typeAny" w:cryptAlgorithmSid="4"
        w:cryptSpinCount="100000" w:hash="UrgUnH3e8g+JF+pZ0azudEQQUYY=" 
        w:salt="dKkOT11EOm/O3alLt8NBbQ=="/>
    

    The hash and salt you'll need to set on your own, you can refer to the Ecma specs and implementation notes for those details, but this is a really good tutorial to just jump right in. But what this does is limit all editing to only content controls.