CustomXmlNode.setTextAsync method (JavaScript API for Office)

Office and SharePoint Add-ins

La dernière version de ce complément Office est disponible dans le référentiel GitHub des compléments.


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

Hosts:

Word

Available in Requirement set

CustomXmlParts

Added in

1.2

See all support details


                      customXmlNodeObj.setTextAsync(text, [asyncContext,] callback(asyncResult);

Name

Type

Description

text

string

Required. The text value of the XML node.

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

Optional. A function that is invoked when the callback returns, whose only parameter is of type AsyncResult.

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 setTextAsync method, you can use the properties of the AsyncResult object to return the following information.

Property

Use to...

AsyncResult.value

Not used.

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 set 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', 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties', function () {
        xmlPart.namespaceManager.addNamespaceAsync('dc', 'http://purl.org/dc/elements/1.1/', function () {

            // Get XML nodes by using an Xpath expression. This results in a call to the host.
            xmlPart.getNodesAsync("/cp:coreProperties/dc:subject", function (getNodesAsyncResult) {
                
                // Get the first node returned by using the Xpath expression. This will be the subject element in this example.
                var subjectNode = getNodesAsyncResult.value[0];
                
                // Set the text value of the subject node and use the asyncContext. This results in a call to the host. 
                // The results are logged to the browser console. 
                subjectNode.setTextAsync("newSubject", {asyncContext: "StateNormal"}, function (setTextAsyncResult) {
                   console.log("The status of the call: " + setTextAsyncResult.status);
                   console.log("The asyncContext value = " + setTextAsyncResult.asyncContext);
                });
            });
        });
    });
});

A checkmark (√) 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 pour Bureau Windows

Office Online
(in browser)

Office pour iPad

Word

Available in requirement sets

CustomXmlParts

Minimum permission level

ReadWriteDocument

Add-in types

Task pane

Library

Office.js

Namespace

Office

Version

Changes

1.1

Added setTextAsync.

Afficher: