Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Connecting to Jira using C#

4.83/5 (8 votes)
20 Apr 2014CPOL2 min read 134.4K  
This tip explains how to connect to Jira from C#

Introduction

In my current project, we use Atlassian Jira for issue tracking and also for Agile Project Management. We moved to Jira recently, though Jira provides lot of built-in reports, our requirement forced me to look for ways to connect to Jira to get data. This tip talks about the ways to connect to Jira using Atlassian SDK.

Getting the SDK

Atlassian Jira .NET SDK can be installed using nuget from this link or using the following command in Package Manager Console available in Visual Studio:

Install-Package Atlassian.SDK -Version 2.2.0

Using the Code

The following code shows how to connect to Jira after referencing Atlassian Jira .NET SDK in Visual Studio project.

C#
using Atlassian;

namespace JiraResultsApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Jira jiraConn = new Jira("http://yourjiraurl.com/", jUserID, jPassword);
            string jqlString = PrepareJqlbyDates("2014-03-01", "2014-03-31");
            IEnumerable<Atlassian.Jira.Issue><atlassian.jira.issue> 
                jiraIssues = jiraConn.GetIssuesFromJql(jqlString , 999);

            foreach(var issue in jiraIssues)
            {
                 System.Console.WriteLine(issue.Key.Value +" -- "+ issue.summary);
            }
         }

        static string PrepareJqlbyDates(string beginDate, string endDate)
        {
            string jqlString = "project = PRJ AND status = Closed AND 
            resolved >= "+beginDate+" AND resolved <= "+ endDate;
            return jqlString;
        }
    }
}

Code Explanation

  • Include "using Atlassian"
  • Create Jira connection instance using the following line:
    C#
    Jira jiraConn = new Jira("http://yourjiraurl.com/", jUserID, jPassword);
    
  • This instance is used to retrieve the data from Jira.
  • Though we are creating the Jira Connection here, the actual connection will not happen here. Instead it happens when we try to retrieve data in the following line:
    C#
    IEnumerable<Atlassian.Jira.Issue>?<atlassian.jira.issue> jiraIssues = jiraConn.GetIssuesFromJql(jqlString , 999);
  • The method GetIssuesFromJql takes two parameters, first: the JQL String and then the maximum number of issues to return from the search.
  • Instead of JQL, Jira also provides another way to get data using the filters as shown below:
    C#
    jiraConn.GetIssuesFromFilter("My Filter"); 
  • Once we get the list of issues, we can iterate through the issues and get each issue details.
  • We also can use GetIssue("IssueID") to get specific issue details.
  • Apart from getting the information, the SDK also provides other methods such as:
    • CreateIssue: Create an issue
    • GetProjects: Get list of projects in the URL

Custom Fields

  • One problem that you notice while accessing fields information is that, not all the fields will be accessible directly. This is because Jira allows us to add custom fields by Project.
  • So, in order to access Custom Fields, we have to use "CustomFields" as shown below:
  • Before accessing any custom field, we should check whether they are null or not.
    C#
    string jStoryPoints = issue.CustomFields["Story Points"] != null ? issue.CustomFields["Story Points"].Values[0] : "";
    
    string jDeveloper = issue.CustomFields["Developer"] != null ? 
        issue.CustomFields["Developer"].Values[0] : "";
    
    string jTester = issue.CustomFields["Tester"] != null ? issue.CustomFields["Tester"].Values[0] : "";  

Connecting using SOAP Service

  • Apart from connecting using SDK, we can also connect using the Jira SOAP service.
  • This tip explains how to connect using the SOAP service.
  • The methods supported by both SDK and SOAP service are same.

Points of Interest

I learned how to connect to Jira using both SDK and also using SOAP service.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)