Click here to Skip to main content
Click here to Skip to main content

Tagged as

Connecting to Jira using C#

, 20 Apr 2014
Rate this:
Please Sign up or sign in to vote.
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.

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

Share

About the Author

chandu_vrs

United States United States
No Biography provided

Comments and Discussions

 
QuestionThank you! PinmemberMember 412224123-Jul-14 7:54 
QuestionPerfect work PinmemberMember 1092203023-Jul-14 2:17 
GeneralThanks a lot! PinmemberK.Meister20-May-14 21:04 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 21 Apr 2014
Article Copyright 2014 by chandu_vrs
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid