1

Is it possible to add Hours to a task via the API?

I was looking through the API documentation and even tried a couple tests agains the API itself. I keep getting back:

{
    "data": {
        "ID": null, 
        "objCode": "HOUR"
    }
}

Not a terribly helpful response.

We are currently using the API to integrate with our Git servers and we would also like to be able to include hours in our commit messages so that we can keep track of our hours without breaking our development flow. We are not so concerned about hours for specific users as much as we are about getting total development time that goes into a task.

AJ Michels
  • 11
  • 1

2 Answers2

0

Yes you can add hours through the AtTask API. It would look something like this using a URI.

/attask/api/v4.0/hour?sessionID=****&projectID=4f9afa8d0005ff481ff120f4328b312e&hours=5

It appears you might be trying to create an hour without any "hours" defined. In AtTask when you enter an hour of 0 (or null / undefined) it will not insert an hour but rather remove any other hours attached to the same object.

Example: You enter 5 hours for Project A You then go back and edit the 5 to be 0 hours for Project A

While both of those are initiated as an Add or Edit operation it will remove the hour entries for Project A.

I hope this helps.

  • Is that a GET request? When I try what you described `/attask/api/v4.0/hour?sessionID=*****&projectID=*****&hours=4` I get `{"error": {"class": "java.lang.UnsupportedOperationException", "message": "you must specify an action"}}` With a 405 METHOD NOT ALLOWED status code. I also tried the same thing as a PUT and POST request and both returned 411 LENGTH REQUIRED. – AJ Michels Mar 07 '14 at 22:11
  • The example above was ran within a browser. If you are doing something similar you add method=post to the url and I believe that will fix your problem. – Ryan Hamilton Mar 10 '14 at 16:02
0

Here is what worked for me using C#

[HttpGet]
public void AddHoursToProject()
{
    try
    {
        var postData = new Dictionary<string, string>();
        postData.Add("taskID", TestTask);
        postData.Add("hours", "6");

        var httpResponse = Post_HttpResponseMessage_FormUrlEncodedContent("v6.0/HOUR", postData);
    }
    catch (Exception ex)
    {

    }

private HttpResponseMessage Post_HttpResponseMessage_FormUrlEncodedContent(string taskToPerform, IEnumerable<KeyValuePair<string, string>> postData)
{
    HttpResponseMessage responseMessage;

    using (var apiManagementSystem = new HttpClient())
    {
        apiManagementSystem.BaseAddress = new Uri("https://xxx.preview.workfront.com/attask/api/");
        apiManagementSystem.DefaultRequestHeaders.Clear();

        var jsonMediaType = new MediaTypeWithQualityHeaderValue("application/json");
        apiManagementSystem.DefaultRequestHeaders.Accept.Add(jsonMediaType);
        apiManagementSystem.DefaultRequestHeaders.Add("SessionID", "xxxxxx");

        HttpContent httpContent = new FormUrlEncodedContent(postData);

        responseMessage = apiManagementSystem.PostAsync(taskToPerform, httpContent).Result;
    }
    return responseMessage;
}
Brandon Hunt
  • 67
  • 2
  • 10
  • This will add hours for the user that the session ID belongs to. In order to add hours on the behalf of somebody else you will need an API key and the owner id of who the hours belong to (at least that is how I did it). Here is the C# code. postData.Add("ownerID", OwnerID); postData.Add("apiKey", ApiKey); – Brandon Hunt Feb 17 '17 at 03:42