Brijen Shah
Brijen Shah

Reputation: 53

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

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.

Upvotes: 0

Views: 351

Answers (1)

starian chen-MSFT
starian chen-MSFT

Reputation: 33698

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.

Upvotes: 1

Related Questions