Welcome to MSDN Blogs Sign in | Join | Help

How to add work item programmatically

using System;

using System.Collections;

using System.Xml;

using System.Collections.Generic;

using System.Text;

using Microsoft.TeamFoundation.Client;

using Microsoft.TeamFoundation.WorkItemTracking.Client;

 

class LogBugs

    {

        Project proj = null;

        public LogBugs()

        {

           

            string serverName = "<<ServerIPAddress>>";

            string projectName = "<<ProjectName>>";

 

            Console.WriteLine("Connecting to {0}...", serverName);

// Popup the authentication dialog

            TeamFoundationServer tfs = new TeamFoundationServer(serverName, new UICredentialsProvider());

            tfs.EnsureAuthenticated();

 

 

 

            WorkItemStore store = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));

            proj = store.Projects[projectName];

 

        }

        /// <summary>

        /// Adds a new work item to the Team System project

        /// </summary>

        public  string AddNewWorkItem(string type, string title, string desc, string source, string severity, string bugType, string rootCauseCategory, string rootCauseAnalysis)

        {

            WorkItemType wit = proj.WorkItemTypes[type];

            WorkItem wi = new WorkItem(wit);

 

            wi.Title = title;

            wi.Description = desc;

 

            wi["Assigned To"] = "<<Assigned To>>";

            wi["Severity (G)"] = severity;

            wi["Bug Type (G)"] = bugType;

            wi["Root Cause (G)"] = rootCauseCategory;

            wi["Root Cause Analysis (G)"] = rootCauseAnalysis;

            wi["Source (G)"] = "<<Source>>";

 

            wi.AreaPath = "<<Area Path>>";

            wi.IterationPath = @"<<IterationPath>>";

 

 

            if (!ValidityCheck(wi))

            {

                Console.WriteLine("Unable to save, Invalid Fields");

                return "0";

            }

            else

            {

              wi.Save();

                Console.WriteLine("Successfully saved the workitem");

 

                return wi.Id.ToString();

            }

        }

 

        /// <summary>

        /// Validates the field before saving.

        /// </summary>

        private static bool ValidityCheck(WorkItem wi)

        {

            ArrayList invalidFields = wi.Validate();

 

            if (invalidFields.Count == 0)

            {

                return true;

            }

            else

            {

                foreach (Field f in invalidFields)

                {

 

                    Console.WriteLine("Invalid Field Found '{0}': {1}", f.Name, f.Status.ToString());

                    Console.WriteLine("Current Value: '{0}'", f.Value);

                    Console.WriteLine();

                }

                return false;

            }

        }

 

    

    }

Published Monday, December 03, 2007 12:49 AM by Prakash

Comments

# Free People Searches &raquo; How to add work item programmatically

Anonymous comments are disabled
 
Page view tracker