How To Create Custom Check-In Policy

21/09/2008

How To Create Custom Check-In Policy

Create a new Visual C# class library project.
Add an assembly reference to System.Windows.Forms.dll. You use this assembly to display message boxes.
Add an assembly reference to Microsoft.TeamFoundation.VersionControl.Client.dll. By default, this is installed in the following folder:\Program Files\Visual Studio 2008 Team Foundation Server\Tools
Replace your skeleton class code implementation with the following source. Note that the class derives from the PolicyBase base class and is marked as serializable.

Implement abstract class for ‘PolicyBase’

public override string Description
    {
        get { return "Remind users to add meaningful comments to their checkins"; 
    }

This is a string that is stored with the policy definition on the source control server. If a user does not have the policy plug-in installed, this string is displayed.
You can use this to explain to the user how they should install the policy plug-in.

public override string InstallationInstructions
{
    get { return "To install this policy, read InstallInstructions.txt."; }
}

This string identifies the type of policy.
It is displayed in the policy list when you add a new policy to a Team Project.

public override string Type
    {
        get { return "Custom Checkin Policy"; }
    }

This string is a description of the type of policy. It is displayed when you select the policy in the Add Check-in Policy dialog box.

public override string TypeDescription
    {
        get { return "This policy will prompt the user to decide whether or not they should be allowed to check in."; }
    }

This method is called by the policy framework when you create a new check-in policy or edit an existing check-in policy.
You can use this to display a UI specific to this policy type allowing the user to change the parameters of the policy.

public override bool Edit(IPolicyEditArgs args)
   {
       // Do not need any custom configuration
       return true;
   }

This method performs the actual policy evaluation. It is called by the policy framework at various points in time
when policy should be evaluated. In this example, the method is invoked when various asyc events occur that may have      invalidated the current list of failures.

public override PolicyFailure[] Evaluate()
    {
        string proposedComment = PendingCheckin.PendingChanges.Comment;
        if (String.IsNullOrEmpty(proposedComment))
        {
            return new PolicyFailure[] {
                new PolicyFailure("Please provide some comments about your check-in", this) };
        }
        else
        {
            return new PolicyFailure[0];
        }
    }

This method is called if the user double-clicks on a policy failure in the UI. In this case a message telling the user to supply some comments is displayed.

public override void Activate(PolicyFailure failure)
    {
        MessageBox.Show("Please provide comments for your check-in.", "How to fix your policy failure");
    }

This method is called if the user presses F1 when a policy failure is active in the UI. In this example, a message box is displayed.

public override void DisplayHelp(PolicyFailure failure)
    {
        MessageBox.Show("This policy helps you to remember to add comments to your check-ins.", "Prompt Policy Help");
    }

The Full Code:

using System;
using System.Windows.Forms;
using Microsoft.TeamFoundation.VersionControl.Client;

[Serializable]
public class CheckForCommentsPolicy : PolicyBase
{
    public override string Description
    {
        get { return "Remind users to add meaningful comments to their checkins"; 

    }

    public override string InstallationInstructions
    {
        get { return "To install this policy, read InstallInstructions.txt."; }
    }

    public override string Type
    {
        get { return "Custom Checkin Policy"; }
    }

    public override string TypeDescription
    {
        get { return "This policy will prompt the user to decide whether or not they should be allowed to check in."; }
    }

    public override bool Edit(IPolicyEditArgs args)
    {
        // Do not need any custom configuration
        return true;
    }

    public override PolicyFailure[] Evaluate()
    {
        string proposedComment = PendingCheckin.PendingChanges.Comment;
        if (String.IsNullOrEmpty(proposedComment))
        {
            return new PolicyFailure[] {
                new PolicyFailure("Please provide some comments about your check-in", this) };
        }
        else
        {
            return new PolicyFailure[0];
        }
    }

    public override void Activate(PolicyFailure failure)
    {
        MessageBox.Show("Please provide comments for your check-in.", "How to fix your policy failure");
    }

    public override void DisplayHelp(PolicyFailure failure)
    {
        MessageBox.Show("This policy helps you to remember to add comments to your check-ins.", "Prompt Policy Help");
    }
}

Register your custom Checkin Policy :

  1. Start Regedit.exe and locate the following key HKEY_LOCAL_MACHINE\Software\Microsoft\VisualStudio\9.0\TeamFoundation\SourceControl\Checkin Policies
    The registered policies are listed in the right pane.
  2. Right-click in the right-hand pane, point to New, and then click String Value.
  3. Type the name of your custom policy dynamic link library (DLL), without the DLL extension; Custom Checkin Policy in the above example.
    Important: The new string name must match your DLL filename exactly, without the DLL extension.
  4. Double-click the new string value and set its value to the fully qualified path and filename to the .dll containing your custom policy.

Now you can add your new check-in policy to Team Project.

Add comment
facebook linkedin twitter email

Leave a Reply

3 comments

  1. EneltnomE01/03/2009 ב 12:40

    Here is a look the best [url=http://king-xxx.blogspot.com//][color=red][b]site[/b][/color][/url] in 2008 for adults.
    I do not know the right sort of a link and it is not clear to you at the forum how to do it.
    If you are under 18 do not go to links

  2. Allobbythot14/08/2009 ב 11:28

    [url=obamanews.redxhost.com/4a851d28ef118.html]bitdefender online scan[/url]

  3. Pawan671930/11/2010 ב 16:03

    What does return new PolicyFailure[0] do ?? why is this required