YouTrackSharp: A .NET Client for YouTrack

Posted on by Hadi Hariri

On and off I’ve been working on a .NET library that is a wrapper around YouTrack ReST API’s. YouTrack offers quite a lot of possibilities when it comes to interacting with it. I’ve previously blogged about how we can easily create some HTML 5 graphs using nothing but the ReST API and some jQuery. Of course, we could also take advantage of this API from .NET using merely an HTTP client.

YouTrackSharp

What YouTrackSharp brings to the table is a compact interface to work with YouTrack without worrying about things like cookies, URL’s and other HTTP concerns. It abstracts all that way to provide a simple series of classes to work with YouTrack.

Here’s a sample of a command line app I just wrote to make feature logging easier for ReSharper:

var connection = new Connection(“youtrack.jetbrains.net”);

connection.Authenticate(“username”, “password”);

var issueManagement = new IssueManagement(connection);

var issue = new Issue
{
Assignee = “ReSharperProjectManager”,
Summary = summary,
Description = description,
ProjectShortName = “RSRP”,
Type = “Feature”
};

issueManagement.CreateIssue(issue);

Console.WriteLine(“Feature request logged”);

Currently it allows you to Create Issues, Upload Attachments, Get Issues, Apply Commands to issues as well as a bunch of tasks on Projects. I’m adding features as and when I can, so if there’s something you’d like, log it here.

How to use it

YouTrackSharp is available as a NuGet package and source code is available on GitHub. To use it, just do:

install-package YouTrackSharp

You first need to create a Connection object (providing host) and then either a IssueManagement or ProjectManagement based on what you need to do. The classes are simple and the methods should be self explanatory. The project also comes with tests using MSpec which describe many scenarios. If you have any questions, shout!

Ruby

If you are a Ruby developer, know that Anna Zhdan, a colleague at JetBrains, has also started working on a Ruby library for YouTrack, which is available on GitHub

Comments below can no longer be edited.

35 Responses to YouTrackSharp: A .NET Client for YouTrack

  1. AntonK says:

    January 17, 2012

    Thanks for sharing!
    As far as I understand https://github.com/hhariri/YouTrackSharp was forked from https://github.com/JetBrains/YouTrackSharp… so which branch is official and in sync with YouTrack releases?

  2. Hadi Hariri says:

    January 18, 2012

    Hi Anton

    The one on JetBrains is the official one now. We moved it under the company account.

  3. AntonK says:

    January 18, 2012

    I’ve already found your note at http://hadihariri.com/2011/08/17/youtracksharp-and-youtrackforresharper-on-jetbrains-github/ , saying that you’ve transferred the YouTrackSharp project over to the JetBrains account on github, so https://github.com/JetBrains/YouTrackSharp is the “official version” 🙂

  4. KevinTran says:

    February 19, 2013

    Nice post!

  5. Mike says:

    August 9, 2013

    Do you have any examples of creating issues with custom fields?

    • Hadi Hariri says:

      August 9, 2013

      YouTrackSharp is now based on dynamic type so any field you add (custom) will automatically be persisted. In fact expect for a couple of fields, everything else is custom (dynamic)

  6. RichieB says:

    October 10, 2013

    Great piece of work, especially using dynamics.

    One question I have is how do I translate the date and time values (i.e. created) into a C# datetime? Initially I thought it was ticks but that isn’t anywhere close. I’m creating a tray icon application that pops up a balloon whenever an issue has been logged within the past couple of minutes and just need to compare the time that the issue was created to Now.

    Thanks

    • Hadi Hariri says:

      October 11, 2013

      Thanks. Make sure they are in the same timezone. How are you converting it?

  7. Tom Fanning says:

    October 25, 2013

    So, now it’s all using C# dynamics, it’s not so self explanatory any more. Where’s the documentation?

    • Hadi Hariri says:

      October 25, 2013

      Docs for?

      • David Wimbley says:

        November 9, 2013

        What I think Tom means is that using the above code sample paired with the latest version of YouTrackSharp from NuGet, the sample provided in this article no longer works.

        I think the only change needed to the above sample is to make the code not use object initialization assignment method.

        Change

        var issue = new Issue
        {
        Assignee = “ReSharperProjectManager”,
        Summary = summary,
        Description = description,
        ProjectShortName = “RSRP”,
        Type = “Feature”
        };

        To

        dynamic issue = new Issue();
        issue.Assignee = “ReSharperProjectManager”;
        issue.Summary = “summary”;
        issue.Description = “description”;
        issue.ProjectShortName = “RSRP”;
        issue.Type = “Feature”;

        And I think that solves all the issues that people will have going forward.

        • Hadi Hariri says:

          November 9, 2013

          Ah yes. Sorry. i think time to update the post.

  8. Ivan Sakharov says:

    November 15, 2013

    Is there a way to get timetracking data for the selected issue? Someting like issueManagement.GetCommentsForIssue() but for work items of Issue?

    • Hadi Hariri says:

      November 16, 2013

      I think the latest version has support for Time tracking info. If you look at the tests there should be sample usage.

      • Ivan Sakharov says:

        November 18, 2013

        Firstly I would like to say thank you for your hard work. Your program has saved me a lot of time.
        But I can not find the methods of object Issue for reading timetrack info. And I can not find samples you specified. I’m looking at version https://github.com/JetBrains/YouTrackSharp/tree/master/src/YouTrackSharp/Issues. Perhaps I need to look somewhere else?
        I made workaround of this problem by creating and using in my programm a class Workitem.cs that is similar to the Comment.cs. But if there is a newer version of the program that would be better.

        • Hadi Hariri says:

          November 18, 2013

          @Ivan,

          Sorry, my bad. Thought it supported it. If you’d like to add support, I’d be happy to accept a Pull Request.

  9. abrahamk says:

    January 24, 2014

    How do I determine the value to use for connection? Using “youtrack.jetbrains.net” causes an AuthenticationException (Forbidden).

    • Hadi Hariri says:

      January 24, 2014

      Is your instance on /youtrack? You need to pass path parameter if so.

      • abrahamk says:

        January 27, 2014

        Thank you, that did it. For anyone that needs it, refer,

        http://youtrack.codebetter.com/issue/YTSRP-28#comment=48-224

        If someone can point me to an example that populates all attributes for an issue (assignee”, “priority”, “type”, “subsystem”, “state”,”fixVersions”, “affectsVersions”, “fixedInBuild”, “summary”,”description”, “project”, “permittedgroup”) including comments, I would be eternally grateful.

  10. kh says:

    January 6, 2015

    Hello!
    Could you please explain how to get issue state via youtracksharp?

    Thanks in advance!

    • Hadi Hariri says:

      January 28, 2015

      Hi,

      If you get an issue, and just access State property (remember it’s dynamic type), it should return it.

  11. Nikhil says:

    June 12, 2015

    How do I get ‘Time Tracking’ Details for an Issue?

  12. Dave Smith says:

    August 13, 2015

    Hi, does this still work. I made the changes stated by David Wimbley (Thank you!), but now I can’t even connect to the server.
    I have the following:

    var connection = new Connection(“myservername”);
    connection.Authenticate(“root”, “password”);

    ..and I get the error:

    An unhandled exception of type ‘System.Security.Authentication.AuthenticationException’ occurred in YouTrackSharp.dll

    Additional information: Not Found

    • Hadi Hariri says:

      August 13, 2015

      Dave

      I’ll take a look at it. I think some things might have changed.

      • Dave Smith says:

        August 13, 2015

        Thanks. I’ve just cracked it. It needed the port number – Doh!

        >>var connection = new Connection(“servername”,8081)

        However, once I got past that part, I only ever get the response “Forbidden” when I try to create a new issue.
        The user I’m using has all the correct rights etc…

  13. Jason Rodgers says:

    September 1, 2015

    Not sure why I am getting a “Forbidden” error when the CreateIssue method is called in the following code:

    var connection = new Connection(@"xxx.myjetbrains.com",80, false, "youtrack");
    connection.Authenticate("xxx@xxx.com", "xxx");
    var issueManagement = new IssueManagement(connection);
    dynamic issue = new Issue();
    issue.ProjectShortName = "SB";
    issue.Summary = "something";
    issue.Description = "somethingelse";
    issue.Assignee = "xxx";
    issue.Type = "Feature";
    issueManagement.CreateIssue(issue);

    It blows up at the CreateIssue method call and gives me a stacktrace of:
    ” at EasyHttp.Http.HttpClient.ProcessRequest(String filename) in d:\BuildAgent-03\work\f3c987b97b2372ef\src\EasyHttp\Http\HttpClient.cs:line 208
    at EasyHttp.Http.HttpClient.Post(String uri, Object data, String contentType, Object query) in d:\BuildAgent-03\work\f3c987b97b2372ef\src\EasyHttp\Http\HttpClient.cs:line 139
    at YouTrackSharp.Infrastructure.Connection.MakePostRequest(String command, Object data, String accept) in z:\Builds\work\efd2f38838204140\src\YouTrackSharp\Infrastructure\Connection.cs:line 275
    at YouTrackSharp.Infrastructure.Connection.Post(String command, Object data, String accept) in z:\Builds\work\efd2f38838204140\src\YouTrackSharp\Infrastructure\Connection.cs:line 164
    at YouTrackSharp.Issues.IssueManagement.CreateIssue(Issue issue) in z:\Builds\work\efd2f38838204140\src\YouTrackSharp\Issues\IssueManagement.cs:line 99″

    • Jason Rodgers says:

      September 1, 2015

      Baaaaah… nevermind…. I was using the wrong ProjectShortName. It didn’t match the name I had in my project.

      • Valerie Andrianova says:

        September 2, 2015

        Hi Jason,
        We’re glad that you’ve figured it out. Please let us know if you have more questions.

  14. Jason Rodgers says:

    September 17, 2015

    Am I doing something wrong here? GetAllIssuesForProject takes about 15seconds to return its list of issues.

    Here’s the code I am using:

    var issues = _issueTrackingManager.GetAllIssuesForProject("SD", 4);

    This line takes about 15 seconds to return. Kind of a long time. Is this normal?

    • Mariusz Baldowski says:

      January 17, 2016

      Seems to be the fallback with changing it to dynamic types/fields.
      Ended up with writing static types (Because I know which fields I want to use) and using JSON.Net for deserializing.

  15. Richard Simpson says:

    October 14, 2015

    Hi there.

    I am using this NuGet package in one of my applications, but I am having trouble adding comments.

    Is there a way to do this?

    Thanks,

  16. John Rusk says:

    November 2, 2015

    @Richard

    It worked for me to do ApplyCommand, with the command simply “comment” and the body of the comment in the comment parameter of ApplyCommand

  17. Ton says:

    June 15, 2017

    Hi,
    Is there any way to programmatically download issue attachments?

    I’ve tried using HttpWebRequest and passing the authenticationCookie to it, but it always ends up downloading the youtrack loading page instead of the file itself.

    This is an example code:

    string contentType = MimeMapping.GetMimeMapping(fileName);
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.CookieContainer = new CookieContainer();
    req.CookieContainer.Add(GetConnection().GetAuthenticationCookie());
    req.Method = "GET";
    HttpWebResponse response = (HttpWebResponse)req.GetResponse();
    Stream s = response.GetResponseStream();
    StreamReader sr = new StreamReader(s);
    string t = sr.ReadToEnd();
    

    Where variable t always equals to:

    
    <!--[if lte IE 9]>-->
    
    
    <!--[if gt IE 9]>>-->
    
    <!--
    
    
      
      
    
      
      
    
    
    
    
    
    
    
    
    
    
    
    
    
  18. Vladislav says:

    June 15, 2017

    Hi everyone! I was trying to authenticate using this library and everything was OK until I compile my c# project several times and got Authentication Error – Forbidden. It’s crashes on a string Connection.Authenticate. It’s seems to be there is a timeout protection on youtrack server, isn’t it?

    • Natasha Katson says:

      June 19, 2017

      No, there is no timeout protection on youtrack server.

Subscribe

Subscribe for updates