0

I have a custom checkin policy written in c# and I used VSIX project with custom action enabled on install. Installation is working great. If I have the custom checkin policy applied to the team project in TFS 2010 and I uninstalled my policy from the same installer, it is cleaning up the registry and the files, but the source control still have the policy enabled and throws an error Error loading the policy. I want my installer to remove the policy from the source control while uninstalling the policy. How can I achieve this?

I tried to write the following code in OnAfterUninstall event, but it is not doing what I need:

protected override void OnAfterUninstall(IDictionary savedState)
    {
        base.OnAfterUninstall(savedState);
        RemovePolicy();
    }


private void RemovePolicy()
    {
        try
        {
            TfsTeamProjectCollection projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(projectCollectionUri, new UICredentialsProvider());
            projectCollection.EnsureAuthenticated();

            VersionControlServer vcs = projectCollection.GetService<VersionControlServer>();
            List<TeamProject> lstTeamProject = vcs.GetAllTeamProjects(true).ToList<TeamProject>();

            foreach (TeamProject tp in lstTeamProject)
            {
                List<PolicyEnvelope> tc = tp.GetCheckinPolicies().ToList<PolicyEnvelope>();
                var myPolicy = new MyCustomCheckinPolicy();
                TeamProject teamProject = vcs.GetTeamProject(tp.Name);

                foreach (PolicyType policyType in Workstation.Current.InstalledPolicyTypes)
                {
                    if (policyType.Name == myPolicy.Type)
                    {
                        tc.Remove(new PolicyEnvelope(myPolicy, policyType));
                        teamProject.SetCheckinPolicies(tc.ToArray());
                        break;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw new InstallException("My Error Message");
        }
    }
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
PushCode
  • 1,419
  • 3
  • 15
  • 31
  • 1
    So what *is* it doing? Does it throw an error, fail miserably? And are you really sure you want to do this? Any team member removing the policy will remove it from the server! – jessehouwing Feb 20 '13 at 15:42
  • `Any team member removing the policy will remove it from the server!` oops, I didn't know that. In that case, I don't want to do this. Thank you for warning me. – PushCode Feb 20 '13 at 16:38

1 Answers1

0

Because a custom checkin policy needs to be installed on all developer workstations that access the TFS Project, having it deregister itself from the TFS Project upon uninstall will actually remove the policy whenever any of these developers uninstalls it. Which isn't what you want I suspect.

There are ways to ensure the policy is distributed to all team members through the Team Foundation Power Tools, that way, should you need to ship an upgrade or a different policy, you'll be sure all members have it. Only do the deregistration manually through the UI.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341