TFS Helper Class

A helper class that might help, when you are developing with TFS:

using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Net;
using Microsoft.TeamFoundation.Client;

namespace Utilities
{
    public class TFSHelper
    {
        private WorkItemStore MyStore { get; set; }

        public TFSHelper()
        {
            NetworkCredential cred = new NetworkCredential("userName", "password", "domain");
            TeamFoundationServer tfs = new TeamFoundationServer("serverName", cred);
            tfs.EnsureAuthenticated();
            MyStore = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
        }

        /// <summary>
        /// Get AllowedValues by workItemId and fieldName
        /// </summary>
        /// <param name="workItemId"></param>
        /// <param name="FieldName"></param>
        /// <returns></returns>
        public AllowedValuesCollection GetAllowedValues(int workItemId, string FieldName)
        {
            WorkItem WI = MyStore.GetWorkItem(workItemId);
            AllowedValuesCollection states = WI.Fields[FieldName].AllowedValues;
            return states;
        }


        public WorkItemCollection GetWorkItems(string queryName, string projectName)
        {
            Project project = MyStore.Projects[projectName];
            Dictionary<string, string> values = new Dictionary<string, string>();
            values.Add("project", projectName);

            foreach (StoredQuery projectQuery in project.StoredQueries)
            {
                if (projectQuery.Name == queryName)
                {
                    WorkItemCollection workItems = MyStore.Query(projectQuery.QueryText);
                    return workItems;
                }
            }
            throw new Exception("Query does not exists");                     
        }


        public void UpdateWorkItem(int workItemId, string Description, string tag)
        {
            WorkItem WI = MyStore.GetWorkItem(workItemId);
            WI.Open();
            WI.Fields["Description"].Value = Description;
            WI.Fields["tag"].Value = tag;
            WI.Save();
        }
    }
}

 

 

Regards

No Comments