Search code examples
outlookoutlook-2013

Enable/Disable Outlook Addin Programmatically


I have created Add-in for outlook 2013.

I wanted to on / off other addin. So I have used below code to do that.

Outlook.Application OutLook = new Outlook.Application();

if (OutLook.Application.COMAddIns.Item("OutlookAddIn").Connect)
{
 OutLook.Application.COMAddIns.Item("OutlookAddIn").Connect = false;
}
else
{
OutLook.Application.COMAddIns.Item("OutlookAddIn").Connect = true;
}

But it is working fine, only if I open outlook as administrator. For normal user, it is not working fine.

So is there any other way I can achieve this? or I can run only this code as admin?


Solution

  • You need to be a local admin or a power user to be able to modify HKLM. A normal user doesn't have sufficient privileges for modifying the windows registry keys for the add-in. Most probably the add-in was registeredin the HKLM hive, am I right?

    Of course, for a per-machine add-in, you can set the Connect property if your application is run with administrative permissions. But on a system with UAC enabled, in addition to having administrative permissions, the application process must be elevated.

    You can disable the add-in manually in Outlook, even a per-machine add-in. It means that you can add a HKCU entry to get it done. If I look at the registry, the value of 'LoadBehavior' of the Addin under HKEY_CURRENT_USER has changed. But the value of 'LoadBehavior' under HKEY_LOCAL_MACHINE has not changed. This is expected behavior - normal user can't disable the Addin for all users.

    Be aware, in that case the add-in will be disabled for the current user only.

    That is why I'd suggest disabling all features of your add-in at runtime instead of trying to turn it off. At runtime you can check whether a user is authorized to load your add-in or not, based on the authentication results you can decide whether to load the add-in's UI and other business logic (subscribe to Outlook events and etc.) or not. I.e. the add-in will be enabled, but the end user will not notice that.

    Even if you add the LoadBehavior key to the corresponding HKCU hive, the add-in will be loaded next time Outlook is started.