none
Call Settings.SaveAsync() before task pane app is closed or Word document is closed RRS feed

  • Question

  • Greetings,

    I'm investigating the ability to call Settings.SaveAsync() before a task pane app is closed or the Word document is closed that contains the task pane app.  If this is possible, then I won't have to call SaveAsync() every single time I make a change to Settings.

    I would think that this is a very common scenario, but I can't find much about it.  I see that in the "Persist custom settings" code sample, it actually gives an example of how to do this (adding SaveAsync() as a handler to window.onunload).  However, when I try to close the task pane from the code sample project or in my own project, SaveAsync() always fails with a 5001: Internal Error.

    Debugging further, the error is occurring at:

    Error
       at OSF_DDA_RichClientSettingsManager$Write (https://localhost:44301/scripts/office/1.1/word-15.01.debug.js:59:3)
       at writeSerializedSettings (https://localhost:44301/scripts/office/1.1/word-15.01.debug.js:89:3)
       at Anonymous function (https://localhost:44301/scripts/office/1.1/word-15.01.debug.js:74:5)
       at OSF_DDA_DispIdHost_Facade$InvokeMethod (https://localhost:44301/scripts/office/1.1/o15apptofilemappingtable.debug.js:3641:6)
       at Anonymous function (https://localhost:44301/scripts/office/1.1/o15apptofilemappingtable.debug.js:3772:10)
       at functionToRunAtClose (https://localhost:44301/App/Home/Home.js:55:18)

    It looks to me that at this line 59 in word-15.01.debug.js, that window.external.GetContext() is failing. 

    Can anyone provide some assistance to get the code sample to work, or propose an alternate way to achieve saving before the app is closed?

    Saturday, July 26, 2014 2:23 AM

Answers

  • Hi Gabrielle,

    >>1) Uncomment the last six lines in the code above

    2) Hit 'Start' in Visual Studio.  A new Excel workbook is opened with the app running in a task pane.

    3) Add "name" to the Type a setting name text box, and "value" to  the Type a setting value text box in the app.

    4) Hit the Save button in the app.

    5) Close the app.

    6) Go to the Insert tab on the ribbon, then the My Apps dropdown menu and select the app to reopen it.

    7) Type "name" into the Type a setting to get text box and hit the Get button.

    8) The setting is not found, and when I debug, I see that it is the issue that I reported in my original post.<<

    When we debuging the apps for Office, the Office document is read-only like figure below:

    This is the reason why we failed to save the the settings to the current document.

    We can publish the apps for Office to a shared folder to test the code and it works well for me.

    Hope it is hlepful.

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Gabrielle RK Saturday, August 2, 2014 3:55 PM
    Wednesday, July 30, 2014 6:54 AM
    Moderator

All replies

  • Hi Gabrielle,

    >>If this is possible, then I won't have to call SaveAsync() every single time I make a change to Settings.<<

    No, if you want to persist this settings to the document you need to call SaveAsync yourself. You can get more detail information from the statement below:

    The set method creates a new setting of the specified name if it does not already exist, or sets an existing setting of the specified name in the in-memory copy of the settings property bag. After you call the Settings.saveAsync method, the value is stored in the document as the serialized JSON representation of its data type. A maximum of 2MB is available for the settings of each app.

    Important note                     Important                  

    Be aware that the Settings.set method affects only the in-memory copy of the settings property bag. To make sure that additions or changes to settings will be available to your app the next time the document is opened, at some point after calling the Settings.set method and before the app is closed, you must call the Settings.saveAsync method to persist settings in the document.(refer to Settings.set)

    >>  However, when I try to close the task pane from the code sample project or in my own project, SaveAsync() always fails with a 5001: Internal Error.<<

    Did you close the task pane manually or programmatically? Would you mind sharing us more detail info to reprocue this issue. As far as I test, the code sample works well for me. I changed saveToProperyBag method in StorageLibrary.js to below to save the settings to document.

      // Stores the settings in the JavaScript APIs for Office property bag.
        function saveToPropertyBag(key, value) {
    
            // Note that Project does not support the settings object.
            // Need to check that the settings object is available before setting.
            if (Office.context.document.settings) {
                Office.context.document.settings.set(key, value);
                Office.context.document.settings.saveAsync();
            }
            else {
                var unsupportedError = {
                    name: "Error: Feature not supported",
                    message: "The settings object is not supported in this host application."
                };
                throw unsupportedError;
            }
        }
    Since when we debugging the project the Office document is read-only, I publish the sample to a shared catalogs(refer to How to: Create a network share catalog for task pane and content apps).

    Create a new workbook and set the value use Office app store. When I reopened the document, I could get the value stored by last time successfully.

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, July 28, 2014 7:40 AM
    Moderator
  • Hi Fei,

    Thanks very much for the response.  I'm aware of the documentation you posted with regards to the SaveAsync() method.  I understand that it has to be called from my code.  But I would like it to be called on the event of the app being closed.  As I mentioned, the sample project I referenced above says this is possible and gives the code for it, but I can't get it to work.

    I see the change that you made above to the function saveToPropertyBag to save the settings every time you set them.  That is what I would like to avoid.

    What I'm referring to is this part of the code in ...\CodeSample_PersistCustomSettingsWeb\Scripts\CodeSample_PersistCustomSettings.js:

        // TODO: If you wanted to save the settings stored in the app's property  
        // bag before the app is closed - for instance, for saving app state -  
        // add a handler to the Internet Explorer window.onunload event. 
        //window.onunload = function () { 
     
        //    if (Office.context.document.settings) { 
        //        Office.context.document.settings.saveAsync(); 
        //    } 
        //}; 

    Here are my steps:

    1) Uncomment the last six lines in the code above

    2) Hit 'Start' in Visual Studio.  A new Excel workbook is opened with the app running in a task pane.

    3) Add "name" to the Type a setting name text box, and "value" to  the Type a setting value text box in the app.

    4) Hit the Save button in the app.

    5) Close the app.

    6) Go to the Insert tab on the ribbon, then the My Apps dropdown menu and select the app to reopen it.

    7) Type "name" into the Type a setting to get text box and hit the Get button.

    8) The setting is not found, and when I debug, I see that it is the issue that I reported in my original post.

    Tuesday, July 29, 2014 4:51 AM
  • Hi Gabrielle,

    >>1) Uncomment the last six lines in the code above

    2) Hit 'Start' in Visual Studio.  A new Excel workbook is opened with the app running in a task pane.

    3) Add "name" to the Type a setting name text box, and "value" to  the Type a setting value text box in the app.

    4) Hit the Save button in the app.

    5) Close the app.

    6) Go to the Insert tab on the ribbon, then the My Apps dropdown menu and select the app to reopen it.

    7) Type "name" into the Type a setting to get text box and hit the Get button.

    8) The setting is not found, and when I debug, I see that it is the issue that I reported in my original post.<<

    When we debuging the apps for Office, the Office document is read-only like figure below:

    This is the reason why we failed to save the the settings to the current document.

    We can publish the apps for Office to a shared folder to test the code and it works well for me.

    Hope it is hlepful.

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Gabrielle RK Saturday, August 2, 2014 3:55 PM
    Wednesday, July 30, 2014 6:54 AM
    Moderator
  • Thanks Fei.

    It doesn't seem to make sense to me that the 'read-only' status of the document would be the problem because, while locally debugging, the SaveAsync() call is successful when called at other times than in the repro steps I have outlined above.

    For example, in your original code change:

      // Stores the settings in the JavaScript APIs for Office property bag.
        function saveToPropertyBag(key, value) {
    
            // Note that Project does not support the settings object.
            // Need to check that the settings object is available before setting.
            if (Office.context.document.settings) {
                Office.context.document.settings.set(key, value);
                Office.context.document.settings.saveAsync();
            }

    This call to saveAsync() succeeds.

    Also, I've noticed that the code that I'm interested in getting to work:

        // TODO: If you wanted to save the settings stored in the app's property  
        // bag before the app is closed - for instance, for saving app state -  
        // add a handler to the Internet Explorer window.onunload event. 
        //window.onunload = function () { 
     
        //    if (Office.context.document.settings) { 
        //        Office.context.document.settings.saveAsync(); 
        //    } 
        //}; 

    Also succeeds when I right click on the running app and select "Reload".  With this "Reload" operation, the window.onload event handler runs fine and the saveAsync() method succeeds.  It is only if I click the 'x' (close) button on the app that the event handler fails.

    Wednesday, July 30, 2014 3:56 PM
  • Hi Fei,

    I had the chance to set up the network share as you suggested, and as you described, via this method I am able to see the save happening successfully on the close of the app or of the document.  I can't say that I understand why the 'read-only' status of the document while debugging breaks the SaveAsync() call for the onunload event in some cases or not others, but my apologies for questioning your advice.

    I'm curious--are there any other common quirks of 'read-only' that I should know about that cause undesirable behavior?

    Saturday, August 2, 2014 3:59 PM
  • Hi Gabrielle,

    >>I'm curious--are there any other common quirks of 'read-only' that I should know about that cause undesirable behavior?<<

    As far as I know, there is no such document describing for this secnario. If you have any other issues, please feel free to let us know.

    Also links below is helpful to learn about apps for Office developing.

    Overview of apps for Office

    Build apps for Office

    FAQ for developing apps for Office

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 5, 2014 7:07 AM
    Moderator