Friday, February 10, 2012

Team Foundation Server 2010: Enable checkin policy through code

Just a quick code snippet if you want to enable check-in policies through code:

var projectCollectionUri = new Uri("http://tfs2010:8080/tfs/MyCollection");
var projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(projectCollectionUri, new UICredentialsProvider())
projectCollection.EnsureAuthenticated();

var vcs = projectCollection.GetService<VersionControlServer>();

var teamProject = vcs.GetTeamProject("SomeTeamProject");
var workItemPolicy=new WorkItemPolicy();

foreach (PolicyType policyType in Workstation.Current.InstalledPolicyTypes)
{
if (policyType.Name == workItemPolicy.Type)
{
teamProject.SetCheckinPolicies(new PolicyEnvelope[] { new PolicyEnvelope(workItemPolicy, policyType) });
break;
}
}



In this sample I’m enabling the WorkItems policy(which is part of the Microsoft.TeamFoundation.VersionControl.Controls assembly).

No comments: