2

I am currently storing a key value pair in Office.context.documents.settings using the following function:

Office.context.document.settings.set(name, value);

Once the key-value pair is stored , I am relaunching the add-in and trying to fetch the value using the following function -

Office.context.document.settings.get(name);

But the function is returning null instead of the proper value. Does the value stored in document settings persist across multiple sessions of an application or does it get refreshed once we close the application?

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63

2 Answers2

2

Your question doesn't have a lot of detail but there are two common errors when working with settings:

Failure to Load Settings

Prior to reading a given setting, you need to populate the settings object. This is done using refreshAsync():

Office.context.document.settings.refreshAsync(function(){
    Office.context.document.settings.get(name);
});

Side-loaded Add-ins

When you side-load an add-in, Office generates a random ID and assigns it to your add-in. If you remove and re-side-load the add-in, it will generate a new ID. You'll also get two distinct IDs if you side-load the same add-on on two different machines.

This will affect how settings function since settings are keyed by the Add-in ID when they are stored or recalled from a document. For details on how this works (and how to get around it), see Issue with Office.context.document.settings.get.

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
1

The setting isn't being saved because you did not call saveAsync. The set method only saves the setting in memory, not to the file. To save to the file you must first call set, then call:

Office.context.document.settings.saveAsync(callback);

Then when you reload the add-in you will be able to retrieve the settings with get. Here's the documentation page for the saveAsync method: https://dev.office.com/reference/add-ins/shared/settings.saveasync

-Michael, PM for add-ins

Michael Saunders
  • 2,662
  • 1
  • 12
  • 21