CustomXmlNode.getTextAsync method

Asynchronously gets the text of an XML node in a custom XML part.

Hosts: Word
Available in Requirement set CustomXmlParts
Added in 1.2


Name Type Description
asyncContext object Optional. A user-defined object that is available on the AsyncResult object's asyncContext property. Use this to provide an object or value to the AsyncResult when the callback is a named function.
callback object A function that is invoked when the callback returns, whose only parameter is of type AsyncResult.

Callback Value

When the function you passed to the callback parameter executes, it receives an AsyncResult object that you can access from the callback function's only parameter.

In the callback function passed to the getTextAsync method, you can use the properties of the AsyncResult object to return the following information.

Property Use to...
AsyncResult.value Access a string that contains the inner text of the referenced nodes.
AsyncResult.status Indicates the success or failure of the operation.
AsyncResult.error Access an Error object that provides error information if the operation failed.
AsyncResult.asyncContext Access your user-defined object or value, if you passed one as the asyncContext parameter. This property returns undefined if the asyncContext has not been set.


Learn how to get the text value of a node in a custom XML part.

// Get the built-in core properties XML part by using its ID. This results in a call to Word.
Office.context.document.customXmlParts.getByIdAsync("{6C3C8BC8-F283-45AE-878A-BAB7291924A1}", function (getByIdAsyncResult) {
    // Access the XML part.
    var xmlPart = getByIdAsyncResult.value;
    // Add namespaces to the namespace manager. These two calls result in two calls to Word.
    xmlPart.namespaceManager.addNamespaceAsync('cp', '', function () {
        xmlPart.namespaceManager.addNamespaceAsync('dc', '', function () {

            // Get XML nodes by using an Xpath expression. This results in a call to Word.
            xmlPart.getNodesAsync("/cp:coreProperties/dc:title", function (getNodesAsyncResult) {
                // Get the first node returned by using the Xpath expression. 
                var node = getNodesAsyncResult.value[0];
                // Get the text value of the node and use the asyncContext. This results in a call to Word. 
                // The results are logged to the browser console.
                node.getTextAsync({asyncContext: "StateNormal"}, function (getTextAsyncResult) {
                   console.log("Text of the title element = " + getTextAsyncResult.value;
                   console.log("The asyncContext value = " + getTextAsyncResult.asyncContext;

Support details

A capital Y in the following matrix indicates that this method is supported in the corresponding Office host application. An empty cell indicates that the Office host application doesn't support this method.

For more information about Office host application and server requirements, see Requirements for running Office Add-ins.

Office for Windows desktop Office Online (in browser) Office for iPad
Word Y Y Y
Available in requirement sets CustomXmlParts
Minimum permission level ReadWriteDocument
Add-in types Task pane
Library Office.js
Namespace Office

Support history

Version Changes
1.1 Added getTextAsync.