Edit

Share via


Word.CustomXmlPartScopedCollection class

Contains the collection of Word.CustomXmlPart objects with a specific namespace.

Extends

Remarks

[ API set: WordApi 1.4 ]

Examples

TypeScript
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-custom-xml-part-ns.yaml

// Original XML: <Reviewers xmlns='http://schemas.contoso.com/review/1.0'><Reviewer>Juan</Reviewer><Reviewer>Hong</Reviewer><Reviewer>Sally</Reviewer></Reviewers>

// Gets the custom XML parts with the specified namespace URI.
await Word.run(async (context) => {
  const namespaceUri = "http://schemas.contoso.com/review/1.0";
  console.log(`Specified namespace URI: ${namespaceUri}`);
  const scopedCustomXmlParts: Word.CustomXmlPartScopedCollection =
    context.document.customXmlParts.getByNamespace(namespaceUri);
  scopedCustomXmlParts.load("items");
  await context.sync();

  console.log(`Number of custom XML parts found with this namespace: ${!scopedCustomXmlParts.items ? 0 : scopedCustomXmlParts.items.length}`);
});

Properties

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

items

Gets the loaded child items in this collection.

Methods

getCount()

Gets the number of items in the collection.

getItem(id)

Gets a custom XML part based on its ID.

getItemOrNullObject(id)

Gets a custom XML part based on its ID. If the CustomXmlPart doesn't exist in the collection, then this method will return an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

getOnlyItem()

If the collection contains exactly one item, this method returns it. Otherwise, this method produces an error.

getOnlyItemOrNullObject()

If the collection contains exactly one item, this method returns it. Otherwise, this method will return an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

load(options)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNames)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNamesAndPaths)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

toJSON()

Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that's passed to it.) Whereas the original Word.CustomXmlPartScopedCollection object is an API object, the toJSON method returns a plain JavaScript object (typed as Word.Interfaces.CustomXmlPartScopedCollectionData) that contains an "items" array with shallow copies of any loaded properties from the collection's items.

track()

Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you're using this object across .sync calls and outside the sequential execution of a ".run" batch, and get an "InvalidObjectPath" error when setting a property or invoking a method on the object, you need to add the object to the tracked object collection when the object was first created. If this object is part of a collection, you should also track the parent collection.

untrack()

Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You'll need to call context.sync() before the memory release takes effect.

Property Details

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

TypeScript
context: RequestContext;

Property Value

items

Gets the loaded child items in this collection.

TypeScript
readonly items: Word.CustomXmlPart[];

Property Value

Method Details

getCount()

Gets the number of items in the collection.

TypeScript
getCount(): OfficeExtension.ClientResult<number>;

Returns

Remarks

[ API set: WordApi 1.4 ]

getItem(id)

Gets a custom XML part based on its ID.

TypeScript
getItem(id: string): Word.CustomXmlPart;

Parameters

id

string

ID of the custom XML part to be retrieved.

Returns

Remarks

[ API set: WordApi 1.4 ]

getItemOrNullObject(id)

Gets a custom XML part based on its ID. If the CustomXmlPart doesn't exist in the collection, then this method will return an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

TypeScript
getItemOrNullObject(id: string): Word.CustomXmlPart;

Parameters

id

string

Required. ID of the object to be retrieved.

Returns

Remarks

[ API set: WordApi 1.4 ]

getOnlyItem()

If the collection contains exactly one item, this method returns it. Otherwise, this method produces an error.

TypeScript
getOnlyItem(): Word.CustomXmlPart;

Returns

Remarks

[ API set: WordApi 1.4 ]

getOnlyItemOrNullObject()

If the collection contains exactly one item, this method returns it. Otherwise, this method will return an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

TypeScript
getOnlyItemOrNullObject(): Word.CustomXmlPart;

Returns

Remarks

[ API set: WordApi 1.4 ]

load(options)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

TypeScript
load(options?: Word.Interfaces.CustomXmlPartScopedCollectionLoadOptions & Word.Interfaces.CollectionLoadOptions): Word.CustomXmlPartScopedCollection;

Parameters

options

Word.Interfaces.CustomXmlPartScopedCollectionLoadOptions & Word.Interfaces.CollectionLoadOptions

Provides options for which properties of the object to load.

Returns

load(propertyNames)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

TypeScript
load(propertyNames?: string | string[]): Word.CustomXmlPartScopedCollection;

Parameters

propertyNames

string | string[]

A comma-delimited string or an array of strings that specify the properties to load.

Returns

load(propertyNamesAndPaths)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

TypeScript
load(propertyNamesAndPaths?: OfficeExtension.LoadOption): Word.CustomXmlPartScopedCollection;

Parameters

propertyNamesAndPaths
OfficeExtension.LoadOption

propertyNamesAndPaths.select is a comma-delimited string that specifies the properties to load, and propertyNamesAndPaths.expand is a comma-delimited string that specifies the navigation properties to load.

Returns

toJSON()

Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that's passed to it.) Whereas the original Word.CustomXmlPartScopedCollection object is an API object, the toJSON method returns a plain JavaScript object (typed as Word.Interfaces.CustomXmlPartScopedCollectionData) that contains an "items" array with shallow copies of any loaded properties from the collection's items.

TypeScript
toJSON(): Word.Interfaces.CustomXmlPartScopedCollectionData;

Returns

track()

Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you're using this object across .sync calls and outside the sequential execution of a ".run" batch, and get an "InvalidObjectPath" error when setting a property or invoking a method on the object, you need to add the object to the tracked object collection when the object was first created. If this object is part of a collection, you should also track the parent collection.

TypeScript
track(): Word.CustomXmlPartScopedCollection;

Returns

untrack()

Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You'll need to call context.sync() before the memory release takes effect.

TypeScript
untrack(): Word.CustomXmlPartScopedCollection;

Returns