← Back to blog

How to use the Teamwork Projects SDK

Recently, a new version of the Teamwork .Net SDK was released and I’d like to give you a few examples on how this can be used to make development for Teamwork Projects a lot easier when working with .Net.

Thinking out loud
How to use the Teamwork Projects SDK

Using the Teamwork Projects SDK

Recently, a new version of the Teamwork .Net SDK was released and I’d like to give you a few examples on how this can be used to make development for Teamwork Projects a lot easier when working with .Net.

To begin with, we need to add the library. As its currently still listed as pre-release in nuget we need to add the IncludePreRelease param:

install-package Teamwork -IncludePrerelease

As of writing this theres a .net core and .net 4.6+ version available.

Once we have the library loaded we need to get an authentication token to be used with the api and also the base url for the installation. This part can only be partly done with the SDK.

Getting an Access Token to be used with the client

You need to understand how Teamwork’s App Loginflow is working. Once you have that configured and and ready to receive the callback from the loginflow the SDK offers a handy function to make parsing the callback and fetching the actual access token a lot easier:

using Teamwork;
using Teamwork.Shared;
using Teamwork.Shared.Common;

private async Task<Teamwork.Client> HandleOauthAuthentication(string code, string state) { var response = await LoginFlow.TeamworkLoginFlow.GetLoginDataAsync(code);

// Use response to initialize a new instance of the Teamwork APi Client return Client.GetTeamworkClient( pDomain: response.AccountData.Account.URL, pApiKey: response.TokenData.AccessToken, pUseOauth: true); }

Handling callback form AppLoginflow and getting a new client instance

The instance of the Teamwork API client is everything you need to start working with the API and actual data.

// Get all people in installation
var people = client.Projects.People.GetPeopleAsync();

// Get status updates by people in installation var latestStatusMessages = client.Projects.People.GetPeopleStatusAsync();

// Find a person by email var personByMail = client.Projects.People.GetPersonByMailAsync("[email protected]");

// Get all active Projects var projects = client.Projects.Projects.GetAllProjectsAsync(OnlyStarredProjects?)

// Get a specific project and its details by id var project = client.Projects.Projects.GetProject(theID); // You can chose to include tasks, milestones, people etc here

// Get all boards of a project var boards = client.Projects.Boards.GetProjectBoardssAsync(theId);

// return time tracking totals for a given project and user var totals = client.Projects.Time.GetTotals_Project(project,userid)

// Search for anything in Projects // first param is the search term // second is the type we are searching for // available types are task, project, comment, message, notebook, person var results = client.Projects.Projects.Search("Whatever we want to search for", "task")

Examples how to fetch data from Teamwork Projects usind the SDK

We can also add or modify all items in Teamwork Projects. This can be done in a similarly easy way to fetching data. Lets just have a look at how we would add a new task. To add a task we need to have a projectid and optionally a tasklist, both can be retreived from the example calls above.

// Create a new TodoItem instance
var newTask = new TodoItem() {
    Description = "This is a new task we want to add",
    Content = "The Title for my new task"
};

// And add it as task to a project // tasklist id can be left blank, the task will be added to an "Inbox" tasklist // also we can add it as a subtask and assign the parenttask var result = await client.Projects.Projects.AddTodoItem( pTodoItem: newTask, pProjectId: TheProjectId, pTaskListId: TheTaskListId pIsSubTask: IsSubTask pParentTask: ParentTask);

//if we want to we can complete our newly created task right away, //the result will be the taskid of the newly created task var ok = await client.Projects.Tasks.CompleteTask(result.Id);

Add a new task

These are pretty much the basics of what you can do with the SDK, however theres a lot more and its constantly updated so always worth to update when a new version comes out.

A few more examples:

// Lets just like a comment
var ok = await client.Projects.Reactions.LikeItem("comment", "commentId");

// we can even send messages on teamwork chat! var ok = await client.Projects.Chat.SendMessage("the message i want to send", "the RoomId");

// Update your status var ok = await client.Projects.Me.AddNewStatusMessage("Gone Fishin");

// Or a more complex example, create a company, add a person and // finaly create a project for the new company

// first add the company var company = new Company() { Name = "MyNewCompany" }; var companyId = await client.Projects.Companies.AddCompany(company);

// Now add a person and assign it to the newly created company var newPerson = new Person() { EmailAddress = "[email protected]", FirstName = "max", LastName = "miller", CompanyId = companyId }; var ok = await client.Projects.People.AddPerson(newPerson);

// Finally add a project for the newly added company var projectToCreate = new Project() { Name = "My New project", CompanyId = companyid }; var ok = await client.Projects.Projects.AddProject(projectToCreate);

The SDK is available on Github: https://github.com/Teamwork/dotnet .
If you want to add something or request a change or even if you just have an issue, feel free to add things on Github. The developers always answer fast and love to help.