Make VSTO for Outlook solutions run right off the MSI

Published 01 July 05 06:50 PM | madsn

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 on July 2, 2005 06:16 AM:

linked

# Peter Jausovec said on July 13, 2005 02:27 PM:

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.

# Mads said on July 13, 2005 05:27 PM:

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!

# Peter's Blog said on June 9, 2007 03:54 PM:

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

# Jen0s said on September 6, 2007 11:59 AM:

Hey,

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

# abhi said on October 25, 2007 02:47 PM:

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

# Roman said on July 3, 2008 11:17 AM:

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?

# Vivek said on July 4, 2008 06:23 AM:

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

# Bui said on August 23, 2008 03:46 AM:

Can you post a full sample setup project ?

Thanks

Leave a Comment

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

This Blog

<a name="MyWork">!My work!</a>

Funstuff

Goodies

MSCRM Blogs

Sharepoint

Useful reading

Weblogs

Syndication