azure boards create user story using C# code

Murugan Andezuthu Dharmaratnam 121 Reputation points
2021-10-06T09:24:53.373+00:00

In azure devops I can go to board -> sprints -> New work Item -> User Story
and then create a user story. How can I create a User Story from an application using C# Code.

Would be thankful if some one can point me in the right direction on how this can be done.

Developer technologies | C#
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2021-10-07T03:10:01.423+00:00

    @Murugan Andezuthu Dharmaratnam , you could try the following code to create a user story in Azure Devops in a c# console app.

    Please install the nuget-package Microsoft.TeamFoundationServer.Client first of all.

    Code:

    using Microsoft.TeamFoundation.WorkItemTracking.WebApi;  
    using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;  
    using Microsoft.VisualStudio.Services.Common;  
    using Microsoft.VisualStudio.Services.WebApi;  
    using Microsoft.VisualStudio.Services.WebApi.Patch;  
    using Microsoft.VisualStudio.Services.WebApi.Patch.Json;  
    using System;  
      
    namespace How_to_add_user_story  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                CreateUserStory userStory = new CreateUserStory();  
                userStory.CreateUserStoryUsingClientLib();  
      
                Console.ReadKey();  
            }  
      
            
        }  
      
      
      
      
        public class CreateUserStory  
        {  
            readonly string _uri;  
            readonly string _personalAccessToken;  
            readonly string _project;  
      
            /// <summary>  
            /// Constructor. Manually set values to match your organization.   
            /// </summary>  
            public CreateUserStory()  
            {  
                _uri ="https://dev.azure.com/{orgName}";  
                _personalAccessToken = "Token";  
                _project = "A1";   // ProjectName  
            }  
      
            /// <summary>  
            /// Create a user story using the .NET client library  
            /// </summary>  
            /// <returns>Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.WorkItem</returns>      
            public WorkItem CreateUserStoryUsingClientLib()  
            {  
                Uri uri = new Uri(_uri);  
                string personalAccessToken = _personalAccessToken;  
                string project = _project;  
      
                VssBasicCredential credentials = new VssBasicCredential("", _personalAccessToken);  
                JsonPatchDocument patchDocument = new JsonPatchDocument();  
      
                //add fields and their values to your patch document  
                patchDocument.Add(  
                    new JsonPatchOperation()  
                    {  
                        Operation = Operation.Add,  
                        Path = "/fields/System.Title",  
                        Value = "Test UserStory"  
                    }  
                );  
      
                patchDocument.Add(  
                    new JsonPatchOperation()  
                    {  
                        Operation = Operation.Add,  
                        Path = "/fields/Microsoft.VSTS.TCM.ReproSteps",  
                        Value = "Our authorization logic needs to allow for users with Microsoft accounts (formerly Live Ids) - http:// msdn.microsoft.com/library/live/hh826547.aspx"  
                    }  
                );  
      
                patchDocument.Add(  
                    new JsonPatchOperation()  
                    {  
                        Operation = Operation.Add,  
                        Path = "/fields/Microsoft.VSTS.Common.Priority",  
                        Value = "1"  
                    }  
                );  
      
                patchDocument.Add(  
                    new JsonPatchOperation()  
                    {  
                        Operation = Operation.Add,  
                        Path = "/fields/Microsoft.VSTS.Common.Severity",  
                        Value = "2 - High"  
                    }  
                );  
                VssConnection connection = new VssConnection(uri, credentials);  
                WorkItemTrackingHttpClient workItemTrackingHttpClient = connection.GetClient<WorkItemTrackingHttpClient>();  
      
                try  
                {  
                    WorkItem result = workItemTrackingHttpClient.CreateWorkItemAsync(patchDocument, project, "User Story").Result;  
      
                    Console.WriteLine("User Story Successfully Created: User Story #{0}", result.Id);  
      
                    return result;  
                }  
                catch (AggregateException ex)  
                {  
                    Console.WriteLine("Error creating user story: {0}", ex.InnerException.Message);  
                    return null;  
                }  
            }  
        }  
    }  
    

    Result in console and Azure Devops:

    138365-screenshot-2021-10-07-110250.png

    138401-microsoftteams-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.