How to enable a checkin policy via the version control API

I recently needed to test a new checkin policy that I wrote.  In order to do that, I needed to enable a checkin policy for the unit test's team project using the version control API.  I was a little surprised when it wasn't quite as obvious as I had hoped, and I had to poke around in the source to figure it out.  So, I thought I'd post the code snippet.

            using Microsoft.TeamFoundation.TeamFoundation.Client;
            using Microsoft.TeamFoundation.VersionControl.Client;

            TeamFoundationServer tfs = new TeamFoundationServer(http://yourserver:8080);
            VersionControlServer vcs = (VersionControlServer) tfs.GetService(typeof(VersionControlServer));

            TeamProject tp = vcs.GetTeamProject("SomeTeamProject");
            MyCustomPolicy customPolicy = new MyCustomPolicy();
            bool foundPolicy = false;
            foreach (PolicyType policyType in Workstation.Current.InstalledPolicyTypes)
                if (policyType.Name == customPolicy.Type)
                    tp.SetCheckinPolicies(new PolicyEnvelope[] { new PolicyEnvelope(customPolicy, policyType) });
                    foundCustomPolicy = true;

            if (!foundCustomPolicy)
                throw new ApplicationException("Something's wrong -- MyCustomPolicy is not installed");

tags: , , ,

Comments (5)

  1. The Visual Studio Team System User Education Blog on Team Foundation content update is live on MSDN!….

  2. Jason Camp says:

    How could you disable the added check-in policy?

  3. buckh says:

    Jason, you need to get the list of checkin policies, remove the one you don’t want, and then call SetCheckinPolicies() with the new list.

    To clear all of them, call SetCheckinPolicies with null or an empty array.


  4. Jason Camp says:

    Wouldn’t that just remove the check-in policy instead of disabling it? I need a check-in policy disabled so that I can reference it with the "Custom Path" policy.

  5. buckh says:

    Ah, sorry about that.  What you want to do there is to set the Enabled property to false on the PolicyEnvelope.  Call GetCheckinPolicies() on the TeamProject object, set Enabled to false on the policies as desired, and then send the list back into SetCheckinPolicies().


Skip to main content