# 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.

2020-02-12 · Tim Cadenbach · https://www.tcdev.de/blog/how-to-use-the-teamwork-projects-sdk/

---

<div class="">
<h1 id="4c54" class="pw-post-title ix iy iz bo ja jb jc jd je jf jg jh ji jj jk jl jm jn jo jp jq jr js jt ju jv gz" data-selectable-paragraph="">Using the Teamwork Projects SDK</h1>
</div>
<figure class="gj gl jx jy jz ka gf gg paragraph-image">
<div role="button" class="kb kc ct kd ea ke" tabindex="0">
<div class="gf gg jw"><img alt="" class="ea kf kg" src="/blog/img/legacy/how-to-use-the-teamwork-projects-sdk-inline-1.png" width="700" height="279" role="presentation" /></div>
</div>
</figure>
<p id="8e2b" class="pw-post-body-paragraph kh ki iz kj b kk kl km kn ko kp kq kr ks kt ku kv kw kx ky kz la lb lc ld le is gz" data-selectable-paragraph="">Recently, a<span>&nbsp;</span><a class="au lf" href="https://github.com/Teamwork/dotnet" rel="noopener ugc nofollow" target="_blank">new version of the Teamwork .Net SDK</a><span>&nbsp;</span>was released and I&rsquo;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.</p>
<p id="033c" class="pw-post-body-paragraph kh ki iz kj b kk kl km kn ko kp kq kr ks kt ku kv kw kx ky kz la lb lc ld le is gz" data-selectable-paragraph="">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:</p>
<blockquote class="lg lh li">
<p id="d31b" class="kh ki lj kj b kk kl km kn ko kp kq kr lk kt ku kv ll kx ky kz lm lb lc ld le is gz" data-selectable-paragraph=""><em class="iz">install-package Teamwork -IncludePrerelease</em></p>
</blockquote>
<p id="768a" class="pw-post-body-paragraph kh ki iz kj b kk kl km kn ko kp kq kr ks kt ku kv kw kx ky kz la lb lc ld le is gz" data-selectable-paragraph="">As of writing this theres a .net core and .net 4.6+ version available.</p>
<p id="4075" class="pw-post-body-paragraph kh ki iz kj b kk kl km kn ko kp kq kr ks kt ku kv kw kx ky kz la lb lc ld le is gz" data-selectable-paragraph="">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.</p>
<h2 id="da33" class="ln lo iz bo lp lq lr ls lt lu lv lw lx ks ly lz ma kw mb mc md la me mf mg mh gz" data-selectable-paragraph="">Getting an Access Token to be used with the client</h2>
<p id="0e52" class="pw-post-body-paragraph kh ki iz kj b kk mi km kn ko mj kq kr ks mk ku kv kw ml ky kz la mm lc ld le is gz" data-selectable-paragraph="">You need to understand how<span>&nbsp;</span><a class="au lf" href="https://developer.teamwork.com/projects/authentication-questions/how-to-authenticate-via-app-login-flow" rel="noopener ugc nofollow" target="_blank">Teamwork&rsquo;s App Loginflow</a><span>&nbsp;</span>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:</p>
<figure class="mn mo mp mq gr ka">
<div class="m l ct">
<div class="wf ms l">
<pre class="language-csharp"><code>using Teamwork;
using Teamwork.Shared;
using Teamwork.Shared.Common;

private async Task&lt;Teamwork.Client&gt; 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);
}</code></pre>
</div>
</div>
<figcaption class="mt ej gh gf gg mu mv bo b bp bq fc">Handling callback form AppLoginflow and getting a new client instance</figcaption>
</figure>
<p id="69ba" class="pw-post-body-paragraph kh ki iz kj b kk kl km kn ko kp kq kr ks kt ku kv kw kx ky kz la lb lc ld le is gz" data-selectable-paragraph="">The instance of the Teamwork API client is everything you need to start working with the API and actual data.</p>
<figure class="mn mo mp mq gr ka">
<div class="m l ct">
<div class="wg ms l">
<pre class="language-csharp"><code>// 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("max@teamwork.com");

// 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")</code></pre>
</div>
</div>
<figcaption class="mt ej gh gf gg mu mv bo b bp bq fc">Examples how to fetch data from Teamwork Projects usind the SDK</figcaption>
</figure>
<p id="a81a" class="pw-post-body-paragraph kh ki iz kj b kk kl km kn ko kp kq kr ks kt ku kv kw kx ky kz la lb lc ld le is gz" data-selectable-paragraph="">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.</p>
<figure class="mn mo mp mq gr ka">
<div class="m l ct">
<div class="wh ms l">
<pre class="language-csharp"><code>// 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);</code></pre>
</div>
</div>
<figcaption class="mt ej gh gf gg mu mv bo b bp bq fc">Add a new task</figcaption>
</figure>
<p id="577e" class="pw-post-body-paragraph kh ki iz kj b kk kl km kn ko kp kq kr ks kt ku kv kw kx ky kz la lb lc ld le is gz" data-selectable-paragraph="">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.</p>
<p id="38c3" class="pw-post-body-paragraph kh ki iz kj b kk kl km kn ko kp kq kr ks kt ku kv kw kx ky kz la lb lc ld le is gz" data-selectable-paragraph="">A few more examples:</p>
<pre class="language-csharp"><code>// 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 = "max@teamwork.com",
    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);</code></pre>
<figure class="mn mo mp mq gr ka">
<div class="m l ct">
<div class="wi ms l"></div>
</div>
</figure>
<p id="f68b" class="pw-post-body-paragraph kh ki iz kj b kk kl km kn ko kp kq kr ks kt ku kv kw kx ky kz la lb lc ld le is gz" data-selectable-paragraph="">The SDK is available on Github:<span>&nbsp;</span><a class="au lf" href="https://github.com/Teamwork/dotnet" rel="noopener ugc nofollow" target="_blank">https://github.com/Teamwork/dotnet</a><span>&nbsp;</span>.<br />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.</p>
