Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return newly created TFS work item ID using TFS API?

Tags:

c#-4.0

tfs-sdk

Using the TFS API, I am able to create a TFS item, no problem.

What would be the best way for me to know what the Item ID is for the newly created Item?

Thank you,

George

        try
        {
            // Authenticate User Account
            NetworkCredential account = new NetworkCredential(USERNAME, PASSWORD, DOMAIN);
            // for user stories from the team project where the user story will be created.
            Uri collectionUri = new Uri(tfsURI);
            //TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(collectionUri);
            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(collectionUri, account);
            WorkItemStore workItemStore = tpc.GetService<WorkItemStore>();
            Project teamProject = workItemStore.Projects[info.TFSProjectName];
            WorkItemType workItemType = teamProject.WorkItemTypes[info.ItemType];
            // Create the work item. 
            WorkItem userStory = new WorkItem(workItemType);
            userStory.Title = info.Title;
            userStory.Description = info.Description;
            userStory.AreaPath = info.AreaPath;
            userStory.IterationPath = info.IterationPath;
            userStory.Fields["Assigned To"].Value = info.AssignedTo;
            if (info.ItemType == "Task")
            {
                userStory.Fields["Discipline"].Value = info.Discipline;
            }
            else if (info.ItemType == "Bug")
            {
                userStory.Fields["Symptom"].Value = info.Symptom;
                userStory.Fields["Steps To Reproduce"].Value = info.StepsToReproduce;
            }
            else if (info.ItemType == "Change Request")
            {
                userStory.Fields["Justification"].Value = info.Justification;
            }
            // Save the new user story.
            userStory.Save();
            return true;
        }
        catch (Exception ex)
        {
            log.Error("Error Creating TFS Task.", ex);
            return false;
        }
        finally
        {
        }
    }
like image 952
ElMatador Avatar asked Sep 29 '10 18:09

ElMatador


1 Answers

As soon as you save userStory, the ID field will be populated. You should be able to just return userStory.Id.

like image 93
Robaticus Avatar answered Oct 20 '22 03:10

Robaticus