Welcome to MSDN Blogs Sign in | Join | Help

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;
                    break;
                }
            }

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

tags: , , ,

Published Thursday, March 01, 2007 9:28 PM by buckh

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# VSTS Links - 03/06/2007

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

Tuesday, March 06, 2007 9:00 AM by Team System News

# re: How to enable a checkin policy via the version control API

How could you disable the added check-in policy?

Thursday, August 14, 2008 3:10 PM by Jason Camp

# re: How to enable a checkin policy via the version control API

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.

Buck

Monday, August 18, 2008 1:04 PM by buckh

# re: How to enable a checkin policy via the version control API

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.

Tuesday, August 19, 2008 11:11 AM by Jason Camp

# re: How to enable a checkin policy via the version control API

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().

Buck

Tuesday, August 19, 2008 11:41 AM by buckh

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker