Click here to Skip to main content
15,881,204 members
Articles / Programming Languages / C#
Tip/Trick

Connecting to Jira using C#

Rate me:
Please Sign up or sign in to vote.
4.83/5 (8 votes)
20 Apr 2014CPOL2 min read 131.2K   22   14
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionError fetching data from Jira Pin
Member 1379557824-Apr-18 0:54
Member 1379557824-Apr-18 0:54 
QuestionSystem.ServiceModel.EndpointNotFoundException was unhandled Pin
Member 1037284723-Feb-18 6:42
Member 1037284723-Feb-18 6:42 
QuestionCreate Issue with custom field Pin
sekharnani4-Feb-18 22:54
sekharnani4-Feb-18 22:54 
QuestionCould you please share me the details about how to get list of issues from Jira server Pin
Member 129158679-Jan-17 4:09
Member 129158679-Jan-17 4:09 
Question+1 up Pin
Jaxam22-Jun-16 7:29
Jaxam22-Jun-16 7:29 
QuestionConnecting JIRA using C# over https when .p12 certificate is used Pin
Arpit D5-Oct-15 13:04
Arpit D5-Oct-15 13:04 
QuestionAccessing Original Estimate Pin
StevenHawkes29-Mar-15 8:52
StevenHawkes29-Mar-15 8:52 
QuestionUrgent Help!!! - Getting error - The remote server returned an unexpected response: (407) Proxy Authorization Required. Pin
Member 1146569319-Feb-15 18:36
Member 1146569319-Feb-15 18:36 
QuestionGetting all issues Pin
inner444429-Dec-14 21:50
inner444429-Dec-14 21:50 
QuestionCan we get more documentation or exmples of how to use the CreateIssue Function Pin
CBSanchez16-Oct-14 7:10
CBSanchez16-Oct-14 7:10 
QuestionThank you! Pin
Member 412224123-Jul-14 7:54
Member 412224123-Jul-14 7:54 
QuestionPerfect work Pin
Member 1092203023-Jul-14 2:17
Member 1092203023-Jul-14 2:17 
GeneralThanks a lot! Pin
K.Meister20-May-14 21:04
K.Meister20-May-14 21:04 
GeneralRe: Thanks a lot! Pin
Ali BaderEddin1-Jul-15 12:47
Ali BaderEddin1-Jul-15 12:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.