How to enable a checkin policy via the version control API

Buck Hodges

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: , , ,


Leave a comment

Feedback usabilla icon