Microsoft 365 and Office | Development | Office JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I want to update all custom property fields of document with new value if they are present. Here is my code
Word.run(function (context) {
var customDocProps = context.document.properties.customProperties;
// first, load custom properties object
context.load(customDocProps);
return context.sync()
.then(function () {
// now load actual property
var filenameProp = customDocProps.getItemOrNullObject("PW", { matchPrefix: true });
context.load(filenameProp);
return context.sync()
.then(function () {
if (customDocProps.items.length > 0) {
var fields = context.document.body.fields.load("items");
fields.load(["code", "result"]);
return context.sync()
.then(function () {
if (fields.items.length > 0) {
$.each(fields.items, function (i, k) {
$.each(customDocProps.items, function (j, l) {
if (k.code.trim().replace(/\s\s+/g, ' ').split(' ')[1].toLowerCase() == l.key.toLowerCase()) {
//replace if values are different
if (k.result.text != l.value) {
k.result.insertText(k.result.text, l.value);
k.load(["code", "result"]);
return context.sync();
}
else {
return false;
}
}
});
});
}
});
}
});
});
}).catch(function (error) {
debugger;
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});