TFS 2015 Alert API: Create an alert for Team or TF

2019-09-19 05:53发布

Is it possible to setup a Team Alert / TFS Group Alert Programmatically using TFS 2015 APIs or Power Shell script? We have a requirement to create TFS Alert more then ~15 and looking for an option to create alert using api / script instead of manually.

1条回答
疯言疯语
2楼-- · 2019-09-19 06:26

As far as I know, the alerts are used for team project and team project collection level, so the alerts that you created in a team will be listed in the team project’s alerts list. For team project collection level, the alerts will be listed in all team project and team’s alerts list.

Simple code to create alert:

NetworkCredential cred = new NetworkCredential("[user name]", "[password]", "[domain]");
        TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("[collection url]"), cred);
        tpc.EnsureAuthenticated();
        IIdentityManagementService2 ims = tpc.GetService<IIdentityManagementService2>();


        TeamFoundationIdentity identity = ims.ReadIdentity(IdentitySearchFactor.AccountName,"[user name]", MembershipQuery.None,ReadIdentityOptions.None);
        if(identity!=null)
        {
            string s = identity.Descriptor.Identifier;
        }
        IEventService es = tpc.GetService<IEventService>();
        List<Subscription> allSubScrip = es.GetAllEventSubscriptions().ToList();
        DeliveryPreference deliverPreference = new DeliveryPreference();
        deliverPreference.Address = "[email address]";
        deliverPreference.Schedule = DeliverySchedule.Immediate;
        deliverPreference.Type = DeliveryType.EmailHtml;

        string filter = string.Format("\"CoreFields/IntegerFields/Field[Name='ID']/NewValue\"='10'");
        string eventName = string.Format("<PT N=\"A specific work item is changed API team\" />");
        es.SubscribeEvent(identity.Descriptor.Identifier, "WorkItemChangedEvent", filter, deliverPreference, eventName,projectName: "[team project name]");

Note: If you don't know how to specify filter, you could create the sample alerts in web access, then check record in dbo.tbl_EventSubscription table of the collection database.

查看更多
登录 后发表回答