Reputation: 2159
I am developing an Outlook Addin which authenticates with a web service to fetch data from a database and to store data in a database. When the addin starts it queries the web service to figure out if the version of the addin installed is the current version, if not then it unloads the addin from Outlook by way of
Application.COMAddIns.Item("foo").Connect = false;
In order to query the web service it must authenticate with it. Credentials are retrieved from encrypted strings in the Windows registry. These credentials come from a Form object which is run whenver the addin starts up or whenever a query is made to the web service and the username and/or password cannot be retrieved from the registry, usually due to someone having deleted said values.
Whenever credentials are saved those credentials are used to query the web service to check if the addin is the correct version. If it is not then the COM addin is to be disconnected from Outlook.
Whenever the web service is queried for other purposes a query is first made to check if the addin is the correct version. If it is not then the COM addin is to be disconnected from Outlook.
As far as I know disconnecting the addin can only be done from the Outlook.Application object which I so far have only been able to access from my Addin object.
What I need to figure out is how can I disconnect the Outlook Addin or otherwise disable it when I'm not in my Addin object?
Upvotes: 2
Views: 3138
Reputation: 2159
I managed to access the COM object via the context of a Ribbon, so I solved it all by creating a public static method that takes a COMAddIn object as an argument and from there I could do whatever I wanted :)
The reference to all your addins can be referenced via the Context property of a Ribbon like this:
Microsoft.Office.Core.COMAddIns comaddins = ((this.Context as Outlook.Explorer).Application.COMAddIns.Application as Outlook.Application).COMAddIns;
The static method looks like this:
public static void ThisAddIn_CheckVersion(Microsoft.Office.Core.COMAddIn ThisAddIn)
{
var rk = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Office\\Outlook\\Addins\\My Outlook Add-in");
if (rk.GetValue("Username") == null || rk.GetValue("Password") == null)
{
new EditSettingsForm(ThisAddIn).Show();
return;
}
var sc = new MyWebService.WebServiceClient();
sc.ClientCredentials.UserName.UserName = (rk.GetValue("Username") == null ? null : rk.GetValue("Username").ToString());
sc.ClientCredentials.UserName.Password = (rk.GetValue("Password") == null ? null : Encryptor.Decrypt(rk.GetValue("Password").ToString()));
if (sc.GetMyOutlookAddinVersionNumber() != "TESTING")
{
System.Windows.Forms.MessageBox.Show("The version of My Outlook 2013 Add-in you're using is too old. Please update to the latest version at http://www.foo.bar/");
ThisAddIn.Connect = false;
}
sc = null;
}
Upvotes: 2