public ProcessWorkItemTypeField Field_GetWorkItemTypeField()
        {
            ProcessWorkItemTypeField processWorkItemTypeField = null;

            System.Guid processId = Context.GetValue <Guid>("$processId");

            VssConnection connection = Context.Connection;
            WorkItemTrackingProcessHttpClient client = connection.GetClient <WorkItemTrackingProcessHttpClient>();

            processWorkItemTypeField = client.GetWorkItemTypeFieldAsync(processId, _witRefName, _fieldRefName).Result;

            return(processWorkItemTypeField);
        }
        public static ProcessWorkItemTypeField GetField(VssConnection connection, Guid processId, string witRefName, string fieldRefName)
        {
            WorkItemTrackingProcessHttpClient client = connection.GetClient <WorkItemTrackingProcessHttpClient>();

            try
            {
                ProcessWorkItemTypeField item = client.GetWorkItemTypeFieldAsync(processId, witRefName, fieldRefName).Result;

                return(item);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
        public ProcessWorkItemTypeField Field_UpdateWorkItemTypeField()
        {
            UpdateProcessWorkItemTypeFieldRequest newfieldRequest = new UpdateProcessWorkItemTypeFieldRequest()
            {
                DefaultValue = "Blue"
            };

            System.Guid processId = Context.GetValue <Guid>("$processId");

            VssConnection connection = Context.Connection;
            WorkItemTrackingProcessHttpClient client = connection.GetClient <WorkItemTrackingProcessHttpClient>();

            ProcessWorkItemTypeField processWorkItemTypeField = client.UpdateWorkItemTypeFieldAsync(newfieldRequest, processId, _witRefName, _fieldRefName).Result;

            return(processWorkItemTypeField);
        }
        public ProcessWorkItemTypeField Field_AddSystemFieldToWorkItemType()
        {
            string fieldName = "Microsoft.VSTS.Common.Activity";

            ProcessWorkItemTypeField processWorkItemTypeField = null;

            System.Guid processId = Context.GetValue <Guid>("$processId");

            VssConnection connection = Context.Connection;
            WorkItemTrackingProcessHttpClient client = connection.GetClient <WorkItemTrackingProcessHttpClient>();

            //get the list of fields on the work item item
            Console.Write("Loading list of fields on the work item and checking to see if field '{0}' already exists...", fieldName);

            List <ProcessWorkItemTypeField> list = client.GetAllWorkItemTypeFieldsAsync(processId, _witRefName).Result;

            //check to see if the field already exists on the work item
            processWorkItemTypeField = list.Find(x => x.ReferenceName == fieldName);

            //field is already on the work item, so just return it
            if (processWorkItemTypeField != null)
            {
                Console.WriteLine("field found");
                return(processWorkItemTypeField);
            }
            else
            {
                //the field is not on the work item, so we best add it
                Console.WriteLine("field not found");
                Console.Write("Adding field to work item...");

                AddProcessWorkItemTypeFieldRequest fieldRequest = new AddProcessWorkItemTypeFieldRequest()
                {
                    AllowGroups   = false,
                    DefaultValue  = String.Empty,
                    ReadOnly      = false,
                    ReferenceName = fieldName,
                    Required      = false
                };

                processWorkItemTypeField = client.AddFieldToWorkItemTypeAsync(fieldRequest, processId, _witRefName).Result;

                Console.WriteLine("done");

                return(processWorkItemTypeField);
            }
        }
        private static void AddFieldToWorkItemTypeLayout(VssConnection connection, Process process, ProcessWorkItemTypeField field, string workItemTypeRefName)
        {
            FormLayout layout = GetLayout(connection, process, workItemTypeRefName);

            Group customGroup = null;

            // look for the custom group
            foreach (var page in layout.Pages)
            {
                foreach (var section in page.Sections)
                {
                    foreach (var group in section.Groups)
                    {
                        if (group.Label.Equals("custom", StringComparison.OrdinalIgnoreCase))
                        {
                            customGroup = group;
                            break;
                        }
                    }
                }
            }

            // create the group since it does not exist
            if (customGroup == null)
            {
                Group group = new Group()
                {
                    Label   = "Custom",
                    Visible = true
                };

                var firstPage   = layout.Pages[0];
                var lastSection = firstPage.Sections.LastOrDefault(x => x.Groups.Count > 0);

                ConsoleLogger.Log("Creating a group Custom to put the field control in");
                customGroup = CreateGroup(connection, group, process, workItemTypeRefName, firstPage.Id, lastSection.Id);
            }
            else
            {
                ConsoleLogger.Log("Layout group Custom already exists on the work item type");
            }

            // check if field already exists in the group
            Control fieldControl = null;

            foreach (var control in customGroup.Controls)
            {
                if (control.Id.Equals(field.ReferenceName, StringComparison.OrdinalIgnoreCase))
                {
                    fieldControl = control;
                    break;
                }
            }

            // add the field to the group
            if (fieldControl == null)
            {
                Control control = new Control()
                {
                    Id       = field.ReferenceName,
                    ReadOnly = false,
                    Label    = field.Name,
                    Visible  = true
                };

                ConsoleLogger.Log("Adding the field control to the group");
                SetFieldInGroup(connection, control, process, workItemTypeRefName, customGroup.Id, field.ReferenceName);
            }
            else
            {
                ConsoleLogger.Log("Field already added to layout.");
            }
        }
示例#6
0
        public static int Main(string[] args)
        {
            if (args.Length == 0)
            {
                ShowUsage();
                return(0);
            }

            args = SetArgumentsFromConfig(args);

            string org, pat, process, project, refname, name, type, action;
            string witrefname, targetprocess;

            try
            {
                CheckArguments(args, out org, out pat, out project, out refname, out name, out type, out action, out process, out witrefname, out targetprocess);

                Uri baseUri = new Uri(org);

                VssCredentials clientCredentials = new VssCredentials(new VssBasicCredential("username", pat));
                VssConnection  vssConnection     = new VssConnection(baseUri, clientCredentials);

                if (action == "clonewit")
                {
                    Console.WriteLine("Start Validation...");

                    bool val = CloneWitAndProcessValidation(vssConnection, process, targetprocess, witrefname);

                    if (!val)
                    {
                        return(0);
                    }
                }

                //action out all fields
                if (action == "listallfields")
                {
                    var fields = Repos.Fields.GetAllFields(vssConnection);

                    var table = new ConsoleTable("Name", "Reference Name", "Type");

                    foreach (WorkItemField field in fields)
                    {
                        table.AddRow(field.Name, field.ReferenceName, field.Type);
                    }

                    table.Write();
                    Console.WriteLine();

                    return(0);
                }

                //get one field by refname and me all of the processes that field is in
                if (action == "getfield" && (!String.IsNullOrEmpty(refname)))
                {
                    List <ProcessInfo>         processList = Repos.Process.GetProcesses(vssConnection);
                    List <ProcessWorkItemType> witList;

                    var table = new ConsoleTable("Process", "Work Item Type", "Field Name", "Field Reference Name");

                    foreach (var processInfo in processList)
                    {
                        witList = Repos.Process.GetWorkItemTypes(vssConnection, processInfo.TypeId);

                        foreach (var witItem in witList)
                        {
                            ProcessWorkItemTypeField witField = Repos.Process.GetField(vssConnection, processInfo.TypeId, witItem.ReferenceName, refname);

                            if (witField != null)
                            {
                                table.AddRow(processInfo.Name, witItem.Name, witField.Name, witField.ReferenceName);
                            }

                            witField = null;
                        }
                    }

                    table.Write();
                    Console.WriteLine();
                }

                if (action == "getfieldforprojects" && (!String.IsNullOrEmpty(refname)))
                {
                    Console.WriteLine("Getting list of projects and work item types...");
                    Console.WriteLine();

                    var table = new ConsoleTable("Project", "Work Item Type", "Field Reference Name", "Field Name");
                    WorkItemTypeFieldWithReferences field;

                    List <TeamProjectReference> projectList = Repos.Projects.GetAllProjects(vssConnection);
                    List <WorkItemType>         witList     = null;

                    foreach (TeamProjectReference projectItem in projectList)
                    {
                        witList = Repos.WorkItemTypes.GetWorkItemTypesForProject(vssConnection, projectItem.Name);

                        foreach (WorkItemType witItem in witList)
                        {
                            field = Repos.Fields.GetFieldForWorkItemType(vssConnection, projectItem.Name, witItem.ReferenceName, refname);

                            if (field != null)
                            {
                                table.AddRow(projectItem.Name, witItem.ReferenceName, field.ReferenceName, field.Name);
                            }

                            field = null;
                        }
                    }

                    table.Write();
                    Console.WriteLine();

                    field   = null;
                    table   = null;
                    witList = null;
                }

                if (action == "searchfields")
                {
                    var fields = Repos.Fields.SearchFields(vssConnection, name, type);

                    if (fields.Count == 0)
                    {
                        Console.WriteLine("No fields found for name: '" + name + "' or type: '" + type + "'");
                        return(0);
                    }

                    var table = new ConsoleTable("Name", "Reference Name", "Type");

                    foreach (WorkItemField field in fields)
                    {
                        table.AddRow(field.Name, field.ReferenceName, field.Type);
                    }

                    table.Write();
                    Console.WriteLine();

                    return(0);
                }

                //add new field to the organization
                if (action == "addfield")
                {
                    //check to see if the type is a legit type
                    int pos = Array.IndexOf(Repos.Fields.Types, type);

                    if (pos == -1)
                    {
                        var types = Repos.Fields.Types;

                        Console.WriteLine("Invalid field type value '" + type + "'");
                        Console.WriteLine();
                        Console.WriteLine("Valid field types are:");
                        Console.WriteLine();

                        foreach (string item in types)
                        {
                            Console.WriteLine(item);
                        }

                        return(0);
                    }

                    //check and make sure the field does not yet exist
                    var field = Repos.Fields.GetField(vssConnection, refname);

                    if (field != null)
                    {
                        Console.WriteLine("Field already exists");
                        Console.WriteLine();

                        var table = new ConsoleTable("Name", "Reference Name", "Type");

                        table.AddRow(field.Name, field.ReferenceName, field.Type);

                        table.Write();
                        Console.WriteLine();

                        return(0);
                    }

                    WorkItemField newField = Repos.Fields.AddField(vssConnection, refname, name, type);

                    if (newField != null)
                    {
                        Console.WriteLine("Field '" + refname + "' was successfully added");
                    }
                }

                if (action == "listfieldsforprocess")
                {
                    List <FieldsPerProcess> list = Repos.Fields.ListFieldsForProcess(vssConnection, process);

                    var table = new ConsoleTable("Work Item Type", "Name", "Reference Name", "Type");

                    foreach (FieldsPerProcess item in list)
                    {
                        List <ProcessWorkItemTypeField> fields = item.fields;

                        foreach (ProcessWorkItemTypeField field in fields)
                        {
                            table.AddRow(item.workItemType.Name, field.Name, field.ReferenceName, field.Type);
                        }
                    }

                    table.Write();
                    Console.WriteLine();

                    return(0);
                }

                vssConnection = null;
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);

                ShowUsage();
                return(-1);
            }

            return(0);
        }