@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:
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.