Walkthrough: Creating Policy Files 

Visual Studio 2005

In the previous walkthrough, you created a project template from a Windows Forms project. In this walkthrough, you will create a policy file that defines policy for this project.

Before beginning this walkthrough, you should have a good understanding of Visual Studio policy. You should also have completed Walkthrough: Creating a Project Template.

To Create The Policy Files

  1. Open the project you created in the previous walkthrough. If you have not completed the previous walkthrough, finish Walkthrough: Creating a Project Template before starting this walkthrough.

  2. In Solution Explorer, select solution WalkthroughTemplate.

  3. From the Project menu, choose Add New Item, then select Text File. Name this file WalkthroughPolicy.vspolicy and click Add.

  4. From the Project menu, choose Add New Item, then select Text File. Name this file WalkthroughPolicy.vspolicydef and click Add.

    Blank policy actions and policy definitions files are added to your project. Note that these files do not need to be a part of the solution, but they are added here for simplicity.

Two files make up the components of Visual Studio policy: the policy actions file (.vspolicy) and the policy definitions file (.vspolicydef). The Policy Definitions file defines the elements that are recognized by the policy engine and how they are recognized at design-time, and the Policy Actions file defines the rules that apply to those elements as they occur in particular contexts. In this procedure, you will add elements to the Policy Actions file.

To Add Elements to the Policy Actions file

  1. In Solution Explorer, right-click WalkthroughPolicy.vspolicy and choose Open.

    WalkthroughPolicy.vspolicy opens.

  2. Add the following line of code to WalkthroughPolicy.vspolicy:

    <?xml version="1.0" encoding="utf-8"?>
  3. On the next line, add the following elements:

    <PolicyActions Version="8.0" xmlns="http://schemas.microsoft.com/VSPolicy/PDLSchema">

    The PolicyActions node is the root node for this file and contains all other elements.

  4. Within the PolicyActions node, add the following elements:


    These nodes identify your custom policy definitions file, WalkthroughPolicy.vspolicydef, and the vside.vspolicydef file as the files from which definitions for this file will be read. The vside.vspolicydef file ships with Visual Studio and contains definitions for most standard Visual Studio elements such as project, project elements, Toolbox items, etc.

  5. After the Definitions node, add the following line of code to define the default behavior:

    <DefaultSettings DefaultAction="Include" Order="ExcludeFirst" PolicyMode="Restrictive"></DefaultSettings>

    For more information about the DefaultSettings node, see DefaultSettings Element (Visual Studio Project Policy).

  6. After the DefaultSettings node, add the following elements:

      <SolutionFolder Id="WalkthroughSolutionFolder">
        <ElementSet DefaultAction="Exclude" Order="IncludeFirst">
      <Project Id="projWalkthroughTemplate">
        <ElementSet DefaultAction="Exclude" Order="IncludeFirst">

    These elements define the behavior allowed for the WalkthroughTemplate project, as well as for a solution folder that your wizard will add (you will implement this in the next walkthrough). Note that this policy actions file will not be valid until you implement the policy definitions file. For more information on policy elements, see Authoring Policy Files in Policy Description Language.

  7. From the File menu, choose Save All.

In this procedure, you will add elements to the Policy Definitions file to create definitions for elements in your solution.

To Add Elements to the Policy Definitions File

  1. In Solution Explorer, right-click WalkthroughPolicy.vspolicydef and choose Open.

    WalkthroughPolicy.vspolicydef opens.

  2. .Add the following lines of code to the WalkthroughPolicy.vspolicydef file:

    <?xml version="1.0" encoding="utf-8"?>
    <PolicyDefinitions Version="8.0" xmlns="http://schemas.microsoft.com/VSPolicy/PDLSchema" >
  3. Within the PolicyDefinitions node, add the following elements:

      <SolutionFolder Id="WalkthroughSolutionFolder">
          <Global Name="ElementType" Value="WalkthroughSolutionFolder"/>
      <Project Id="projWalkthroughTemplate">
          <Global Name="ElementType" Value="projWalkthroughTemplate" />

    These create the definitions for the Solution Folder and project that your wizard will create. Your wizard will also set the Global value that allows the policy engine to identify the project and solution folder. In a real enterprise application, you would create a unique project global for each project or solution folder to which policy is to be applied.

  4. From the File menu, choose Save All.

  5. In Solution Explorer, select WalkthroughTemplate.

  6. In the Properties window, click the ellipses next to the PolicyFile property and navigate to the .vspolicy file that you created at the beginning of this walkthrough. Set this file as the policy file for the project in order to test the policy file.

    The file should add correctly with no policy issues.

  7. In the Properties window, set the policy file to nothing in order to prepare the project for the next walkthrough, which sets the policy file programmatically.

You have now completed policy files for your wizard. The next step is to create the actual wizard and associated files. To do this, continue on to Walkthrough: Creating a Visual Studio Templates and Policy Wizard.

Community Additions