Make VSTO for Outlook solutions run right off the MSI

UPDATE: Installer class with uninstall logic here.

There are a couple of things you've got to do manually to make a Visual Studio Tools for Office 2005 application run straight off the MSI. I'm talking about the Outlook add-in project here, and there is no ClickOnce around.

With the standard setup for Outlook add-in projects in Visual Studio 2005 there are two things missing after you've run the MSI from the included setup project on the client machine:

  • A couple of VSTO and Office related dlls (Should have been loaded from shared but for some reason they need to be in the local dir)
  • Code Access Security settings granting your VSTO for Outlook addin rights to do what it wants

The first one is easy. Simply add a reference to Microsoft.Office.Tools.Common.dll to the Outlook project and it will be included in the setup. Also enable the files Microsoft.Office.Tools.Outlook.dll and Microsoft.VisualStudio.Tools.Applications.Runtime.dll in the Setup project so it is included with the MSI and placed in the target application directory on the client machine.

Then you'll need to handle CAS. You need to create a custom Installer class to do this work. Based on this article I've created the following installer class and added it as a custom action in my Installer.

[RunInstaller(true)]

public class Installer : System.Configuration.Install.Installer

{

    public Installer()

    {

    }

 

    public override void Install(System.Collections.IDictionary stateSaver)

    {

        base.Install(stateSaver);

 

        try

        {

            // Find the machine policy level

            PolicyLevel machinePolicyLevel = null;

            System.Collections.IEnumerator policyHierarchy = SecurityManager.PolicyHierarchy();

 

            while (policyHierarchy.MoveNext())

            {

                PolicyLevel level = (PolicyLevel)policyHierarchy.Current;

                if (level.Label == "Machine")

                {

                    machinePolicyLevel = level;

                    break;

                }

            }

 

            if (machinePolicyLevel == null)

            {

                throw new ApplicationException(

                    "Could not find Machine Policy level. Code Access Security "+

                    "is not configured for this application."

                    );

            }

 

            // Create a new FullTrust permission set

            PermissionSet permissionSet = new NamedPermissionSet("FullTrust");

           

            // Get the install directory of the current installer

            string assemblyPath = this.Context.Parameters["assemblypath"];

            string installDirectory =

                assemblyPath.Substring(0, assemblyPath.LastIndexOf("\\"));

 
            if (!installDirectory.EndsWith(@"\"))
               installDirectory +=
@"\";

            installDirectory += "*";

            IMembershipCondition membershipCondition =

                new UrlMembershipCondition(installDirectory);

 

            // Create the code group

            PolicyStatement policyStatement = new PolicyStatement(permissionSet);

            CodeGroup codeGroup = new UnionCodeGroup(membershipCondition, policyStatement);

            codeGroup.Description = "VSTO Permissions for Objectware AS Plugins";

            codeGroup.Name = "Objectware AS";

 

            // Add the code group

            machinePolicyLevel.RootCodeGroup.AddChild(codeGroup);

 

            // Save changes

            SecurityManager.SavePolicy();

        }

        catch (Exception ex)

        {

            System.Windows.Forms.MessageBox.Show(ex.ToString());

        }

    }

}

 

Filed under:

Comments

# KjellSJ said:

linked

Saturday, July 02, 2005 6:16 AM
# Peter Jausovec said:

Hi Mads,

I tried this custom installer and it doesn't work OK - the problem is that you point to the Addin directory (assemblyPath.Substring(0, assemblyPath.LastIndexOf("\\"))) instead of the assembly file.

Wednesday, July 13, 2005 2:27 PM
# Mads said:

Peter,

You're absolutely right. Actually I corrected this bug in the code, but forgot to update my blogpost. I've added the missing lines of code now and it should be working properly.

thanks for noticing!

Wednesday, July 13, 2005 5:27 PM
# Peter's Blog said:

What you need to install and set security for a VSTO 2005 Outlook add-in.

Saturday, June 09, 2007 3:54 PM
# Jen0s said:

Hey,

good article, but you need to update the article on the one that is working.

Thursday, September 06, 2007 11:59 AM
# abhi said:

Where can I find the updated file , ie. the extra lines of code that is added.

Thursday, October 25, 2007 2:47 PM
# Roman said:

So is the code up there updated? I need to install a Add-in to outlook and a lot of people point here. How would i add this into my solution?

Thursday, July 03, 2008 11:17 AM
# Vivek said:

YOUR SOLUTION DOES NOT WORK AT ALL YOU DONT EXPLAIN THINGS NOR DO YOU ANSWER. ITS WASTE OF TIME

Friday, July 04, 2008 6:23 AM
# Bui said:

Can you post a full sample setup project ?

Thanks

Saturday, August 23, 2008 3:46 AM

Leave a Comment

(required) 
(required) 
(optional)
(required)