1

I'm trying to retrieve a setting within a document which I have previously saved within the same session, however i'm getting settings.getItem is not a function.

I'm attempting to do this via an Word Online session within Chrome, and have been referring to the documentation here, and have also seen this post (but I don't think it fits my specific problem)

As mentioned, saving the setting works fine and when I download the file I can see the setting has been saved.

I'm using react + redux for this, so the snippet below is from the redux action creator:

export function getSetting() {
    //this function tries to find a setting within the document
    return function(dispatch, getState) {
        window.Word.run(
            function(context) {
                let settings = Office.context.document.settings
                var thisSetting 
                try {
                    console.log('attempting to find existing setting for ' + constants.ORG_ID)
                    thisSetting = settings.getItem(constants.ORG_ID)
                    console.log('got ' + thisSetting.value)
                    context.load(thisSetting)
                } catch (error) {
                    thisSetting = null
                    console.log('couldn\'t find setting)
                    console.log(error.message)
                }                    
                return context.sync()
                .then(
                    function() {
                        if (thisSetting) {
                            dispatch(requestSetThisSetting (thisSetting.value))
                            console.log('got setting ' + thisSetting.value)
                        }
                    }
                )             
            }
        )
    }
}

Any ideas on how I can successfully retrieve settings?

Updated code based on answer (below)

I've updated this post with working code based on the answer provided by Rick and Juan (thanks guys!)

export function getSetting() {
    //this function tries to find a setting within the document
    return function(dispatch, getState) {
        let currentSetting = Office.context.document.settings.get(constants.ORG_ID)
        console.log('current setting is ' + currentSetting)
        if (currentSetting) dispatch(requestSetSetting(currentSetting))
    }
}
AndrewO
  • 1,105
  • 4
  • 16
  • 33

1 Answers1

1

It looks like you are kind of mixing the Shared API Office.context.document.settings inside a Word.run from the Word-specific APIs. Take a look at the special Word wrapper API (from ver. 1.4 of the Word-specific APIs): SettingsCollection. This is the context.document.settings object, but the "context" here is the Word.RequestContext object that is passed to Word.run, not the Office.context object. Your code gets a settings object using the Shared API Office.context.document.settings, but then it calls the getItem and load methods from the Word.RequestContext.document.settings.

Under the hood, it is the same settings OOXML in the file, but accessing it through the two different APIs might be problematic.

UPDATE: In light of Juan Balmori's comment, here's some info on reading a setting using the Shared API: Getting the value of a setting. And see here for info on the distinction between the Common and host-specific APIs: JavaScript API for Office.

Rick Kirkham
  • 9,038
  • 1
  • 14
  • 32