none
Office Javascript API - CustomXmlPart Event(e.g. NodeReplaced) is never triggered for word 2013/2016 add-ins. RRS feed

  • Question

  • Hi,

    I am creating a word document task pane add-ins by using Javascript API for Office under the version 2013/2016.

    There is a requirement from the business to listen to the change of custom XML part in the document.

    I can get the custom XML from the document in javascript. However, the event never gets triggered when I make a change to the custom XML part. The code is like:

    Office.context.document.customXmlParts.getByIdAsync("{id}", function (asyncResult) {
            var byIdXmlPart = asyncResult.value;
    
            byIdXmlPart.addHandlerAsync(Office.EventType.NodeInserted, function (eventArgs) { // listen to node Inserted event.
                app.showNotification("A node has been Inserted.");
            });
    
            byIdXmlPart.addHandlerAsync(Office.EventType.NodeDeleted, function (eventArgs) { // listen to node deleted event.
                app.showNotification("A node has been deleted.");
            });
    
            byIdXmlPart.addHandlerAsync(Office.EventType.NodeReplaced, function (eventArgs) { // listen to node replaced event.
                app.showNotification("A node has been replaced.");
            });
    
            byIdXmlPart.getNodesAsync('*', function (nodeResults) { // set XML of each node.
                for (var i = 0; i < nodeResults.value.length; i++) {
                    var node = nodeResults.value[i];
                    node.setXmlAsync("<childNode>" + i + "</childNode>");
                }
            });
        });

    or

    Office.context.document.customXmlParts.getByNamespaceAsync("myNamespace", function (asyncResult) {
                var byNameXmlPart = asyncResult.value;
    
                byNameXmlPart[0].addHandlerAsync(Office.EventType.NodeInserted, function (eventArgs) { // listen to node Inserted event.
                    app.showNotification("A node has been Inserted.");
                });
    
                byNameXmlPart[0].addHandlerAsync(Office.EventType.NodeDeleted, function (eventArgs) { // listen to node deleted event.
                    app.showNotification("A node has been deleted.");
                });
    
                byNameXmlPart[0].addHandlerAsync(Office.EventType.NodeReplaced, function (eventArgs) { // listen to node replaced event.
                    app.showNotification("A node has been replaced.");
                });
    
                byNameXmlPart[0].getNodesAsync('*/*', function (nodeResults) {
                    for (var i = 0; i < nodeResults.value.length; i++) {
                        var node = nodeResults.value[i];
                        node.setXmlAsync("<childNode>" + i + "</childNode>");
                    }
                });
            });


    Wondering if anyone could help?

    Thanks,

    Andy

    Thursday, November 5, 2015 1:57 AM

Answers

  • Hi Andy,

    Based on your code, it seems you delete the CustomXMLPart instead of CustomXMLNode. In this way, I think it would not fire the event. But with Office add-ins APIs, I did not find a method to delete CustomXMLNode. So I delete CustomXMLNode from vba code, but I failed to fire the Office.EventType.NodeDeleted event.

    For this issue, I suggest you submit a feedback in the link below:
    http://officespdev.uservoice.com

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, November 10, 2015 7:47 AM

All replies

  • Hi Andy,

    I could insert a new custom xml part to a word document, but I am not sure how you insert, delete or replace the node. Could you share us how you achieved this?

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Saturday, November 7, 2015 4:16 AM
  • Thanks, Edward.

    The way I've tried to trigger the event:

    // listen to the event
    Office.context.document.customXmlParts.getByNamespaceAsync(myCustomNamespace, function(namespaceResult) {
        namespaceResult.value.addHandlerAsync(Office.EventType.NodeDeleted, function(eventArgs) {
            app.showNotification("A node has been deleted.");
        });
    });
    
    
    //delete
    $("#deleteCustomXMLPart").click(function(e) {
        deleteCustomXMLPart(myCustomNamespace);
    });
    
    function deleteCustomXMLPart(namespaceName) {
        Office.context.document.customXmlParts.getByNamespaceAsync(myCustomNamespace, deletePart);
    }
    
    function deletePart(asyncResult) {
        if (asyncResult.value.length > 0) {
            for (var i = 0; i < asyncResult.value.length; i++) {
                asyncResult.value[i].deleteAsync();
            }
        }
    }

    When I click "#deleteCustomXMLPart" button on the page, the event was not triggered.

    Regards,

    Andy

    Tuesday, November 10, 2015 4:23 AM
  • Hi Andy,

    Based on your code, it seems you delete the CustomXMLPart instead of CustomXMLNode. In this way, I think it would not fire the event. But with Office add-ins APIs, I did not find a method to delete CustomXMLNode. So I delete CustomXMLNode from vba code, but I failed to fire the Office.EventType.NodeDeleted event.

    For this issue, I suggest you submit a feedback in the link below:
    http://officespdev.uservoice.com

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, November 10, 2015 7:47 AM
  • Thanks for your help, Edward.

    It doesn't make sense they provided the event type, but no way to trigger it. Also, the documentation of how to use this is not that clear.

    However, I can get around of this by adding a binding to the content control which map to the node in the custom xml part. And listen to the change of the binding. It works.

    Best regards,

    Andy

    Wednesday, November 11, 2015 6:23 AM
  • Hi Andy,

    Thanks for your sharing. It is so wonderful.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, November 11, 2015 7:31 AM