2

I am using Office.js, javascript and react to create excel add-ins. Wants to enable ribbon icons once user successfully sign in. Used Office dialog API for authentication.

  1. below is the tag from Manifest.xml file used to disable "HomeButton menu" icon initially.

     <Action xsi:type="ShowTaskpane">
        <TaskpaneId>DataDirectId</TaskpaneId>
        <SourceLocation resid="HomeButton.Url" />
     </Action>
     <Enabled>false</Enabled>
    
  2. On Click of Login menu icon from ribbon task pan will open with sign in page which contains "Sign IN and Sign Out" buttons.

  3. On click of sign in button below office dialog API code getting called.

     Office.context.ui.displayDialogAsync(
     dialogLoginUrl,
     { height: 45, width: 40 },
     result => {
         if (result.status === Office.AsyncResultStatus.Failed) {
             displayError(`${result.error.code} ${result.error.message}`);
         } else {
             loginDialog = result.value;
             loginDialog.addEventHandler(
                 Office.EventType.DialogMessageReceived,
                 processLoginMessage,
             );
             loginDialog.addEventHandler(
                 Office.EventType.DialogEventReceived,
                 processLoginDialogEvent,
             );
         }
     },
    

    );

// Handlers:

const processLoginMessage = arg => {
    const messageFromDialog = JSON.parse(arg.message);
    if (messageFromDialog.status === 'success') {
    // below method using to enable ribbon Home button icon
        enableButton();
        loginDialog.close();
        // console.log('login detail==', messageFromDialog.result);
        localStorage.setItem('loggedIn', 'yes');
    } else {
        // Something went wrong with authentication or the authorization of the web application.
        loginDialog.close();
        displayError(messageFromDialog.result);
    }
};

const processLoginDialogEvent = arg => {
    processDialogEvent(arg, setState, displayError);
};
  1. Below is the enableButton method for which took reference from Enable / Disable

// Code from enableButton method

    const enableButton = () => {
    // Tried with this option 1
    OfficeRuntime.ui.getRibbon().then(ribbon => {
        ribbon.requestUpdate({
            tabs: [
                {
                    id: 'ShareTime',
                    controls: [
                        {
                            id: 'BtnNTHome',
                            enabled: true,
                        },
                    ],
                },
            ],
        });
    });
    // Tried with this option 2
    Office.ribbon.requestUpdate({
        tabs: [
            {
                id: 'ShareTime',
                controls: [
                    {
                        id: 'BtnNTHome',
                        enabled: true,
                    },
                ],
            },
        ],
    });
    // Tried with this option 3
    const button = { id: 'BtnNTHome', enabled: true };
    const parentTab = { id: 'ShareTime', controls: [button] };
    const ribbonUpdater = { tabs: [parentTab] };
    Office.ribbon.requestUpdate(ribbonUpdater);
};

Tried with Above 3 options but getting below error: excel-web-16.00.js:26 Uncaught (in promise) RichApi.Error: The API you are trying to use is not available. It may be available in a different scenario. at new n (https://appsforoffice.microsoft.com/lib/1/hosted/excel-web-16.00.js:26:300800) at r.i.processRequestExecutorResponseMessage (https://appsforoffice.microsoft.com/lib/1/hosted/excel-web-16.00.js:26:362097) at https://appsforoffice.microsoft.com/lib/1/hosted/excel-web-16.00.js:26:360202 enter image description here

Please help to resolve this issue . Appreciate your help. Thanks in advance.

sagar
  • 464
  • 4
  • 13
  • @Rick Kirkham, can you please help to check this issue. – sagar Mar 01 '21 at 06:36
  • Did you ever solve this? Having a similar issue.. – henry434 Nov 02 '21 at 14:21
  • @henry434, yes solved by removing tag from manifest and instead that used . e.g: btnloginservice function present in code which gets called from manifest which updates the ribbon function. btnloginservice Please check sample manifest from question: https://stackoverflow.com/questions/69525931/excel-add-ins-on-manifest-loading-showing-error-on-desktop-excel# – sagar Nov 03 '21 at 09:10

0 Answers0