none
Issue persisting bindings in the OOXML RRS feed

  • Question

  • I am creating a Task pane Add-in of Office Word which allow the user to create a Word Document which behaves as a template. I let the user to insert Binding into the Word Document to be replaced by user's data when processing the template.

    I have no problem to insert Bindings and replace the data of the Binding in the Word Document, but if i get the OOXML of the Document's body (i persist it into a database) and then, in a new Word Document, i insert the persisted OOXML i cannot retrieve anymore the Bindings using the Javascript API;  document.bindings.getAllAsync returns an empty array.

    The Bindings are supposed to be persisted in the OOXML or i am using a wrong approach ? There is another way to achieve the same result for my use case ? 

    1. Store the OOXML containing the Bindings

    Word.run(function (context) { var body = context.document.body; var bodyOOXML = body.getOoxml(); return context.sync().then(function () { // story bodyOOXML.value containing the Bindings in a database }); })


    2. Insert the stored OOXML into a new Word Document

    Word.run(function (context) { var body = context.document.body; // ooxml has been retrieved from the database body.insertHtml(ooxml, Word.InsertLocation.end); return context.sync().then(function () { console.log('HTML added to the beginning of the document body.'); }); })

    3. Get the bindings in the new document

    Office.context.document.bindings.getAllAsync(function (asyncResult) {

    var bindingString = ''; for (var i in asyncResult.value) { bindingString += asyncResult.value[i].id + '\n'; } console.log('Existing bindings: ' + bindingString); // does not find any binding });

    Thank you

    Monday, November 28, 2016 3:41 PM

All replies

  • Hi,

    I could reproduce your issue. After some researching and testing, we find that the Binding reference is stored in the webextensions part, not in the body XML part. So when we retrieve the OOXML of the body, it actually does not retrieve the whole binding part. When we insert then, it does not insert the binding, so we are unable to get it.

     

    If you are inserting the binding using Bindings.addFromNamedItemAsync method, we could store the parameters in the database, then get the OOXML. Then insert the OOXML into the new document to insert the content, after that insert a new binding using the same method: Bindings.addFromNamedItemAsync using the stored parameters. Then we could successfully get the binding in the new document.

    If not, I think there is no a proper solution to resolve the issue, i suggest you sumbit a feedback in the link below:

    https://officespdev.uservoice.com/

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 30, 2016 10:10 AM
    Moderator
  • Hi Celeste,

    thank you for your reply.

    I am now inserting the text using setSelectedDataAsync and then immediately set the binding to the selected text.

    Do you know how i can insert a NamedItem where is my document's selection ?

    Office.context.document.setSelectedDataAsync(text, { coercionType: Office.BindingType.Text }, setSelectedDataCallback);
    
    function setSelectedDataCallback(asyncResult) {
      if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
    		Office.context.document.bindings.addFromSelectionAsync(Office.BindingType.Text, { id: id }, addBindingCallback)
      } 
    
    function addBindingCallback(asyncResult) {
      if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
    	deferredPromise.resolve(asyncResult.value.id);
      } 
    }

    Regards,

    Paolo


    Wednesday, November 30, 2016 10:58 AM
  • Hi,

    According to Bindings.addFromNamedItemAsync method,

    For Word, the itemName parameter refers to the Title property of a Rich Text content control. (You can't bind to content controls other than the Rich Text content control.)

     

    You could refer to Word Add-in: Add, bind to, and populate a rich text content control  to add binding to selected range.

     
    function addAndBindControl() {
     
            Office.context.document.bindings.addFromNamedItemAsync("MyContentControlTitle", "text", { id: 'myBinding' }, function (result) {
     
                if (result.status === Office.AsyncResultStatus.Failed) {
     
                    // Error 3006 message: "The named item does not exist."
                    // If the named item doesn't exist, then add and bind to it.
                    if (result.error.code === 3006) {
                        var myOOXMLRequest = new XMLHttpRequest();
                        var myXML;
                        myOOXMLRequest.open('GET', '../../Snippets_BindAndPopulate/ContentControl.xml', false);
                        myOOXMLRequest.send();
     
                        if (myOOXMLRequest.status === 200) {
                            myXML = myOOXMLRequest.responseText;
                        }
                        Office.context.document.setSelectedDataAsync(myXML, { coercionType: 'ooxml' }, function (result) {
                            Office.context.document.bindings.addFromNamedItemAsync("MyContentControlTitle", "text", { id: 'myBinding' });
                        });
                    }
                    else app.showNotification(result.error.name + " " + result.error.code, result.error.message);
                }
            });
        }

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 30, 2016 11:44 AM
    Moderator
  • Hi Celeste,

    again thank you for your reply. I can indeed insert a Rich Text Content Control with a Title and add a binding to it. The title is persisted with the OOXML.

    // Run a batch operation against the Word object model.
    Word.run(function(context) {
    
    // Create a proxy range object for the current selection.
    var range = context.document.getSelection();
    var contentControl = range.insertContentControl();
    contentControl.tag = tag;
    contentControl.title = title;
    
    context.load(contentControl, 'id');
    
    return context.sync().then(function() {
       var id = contentControl.id;
       officeResultDeferred.resolve(id);
     }
    })

    Regards,

    Paolo


    Wednesday, November 30, 2016 4:42 PM