title: attach workitem to build via REST API
body: <p>I want to attach a work item on tfs to a build. i am reading SVN logs which contain the work item number and try to update the actual build to attach them.</p>
  
  <pre><code>workitems.Add(workItemStore.GetWorkItem(workitemid));
  buildDetail.Information.AddAssociatedWorkItems(workitems.ToArray());
  </code></pre>
  
  <p>When I try to hit <code>buildDetail.Information.Save();</code> or <code>buildDetail.Save();</code>, I get an <code>AccessDeniedException</code>.</p>
  
  <p>See <a href="https://stackoverflow.com/questions/36157494/tfs-user-needs-permission-to-update-build-information">another post</a>.</p>
  
  <p>So I wanted to try with REST...
  After loads of pages on MSDN, I concluded there is no .NET Client Library that takes care of builds. It looks like my only option is to patch a json into the TFS:</p>
  
  <blockquote>
    <p>PATCH https://{instance}/DefaultCollection/{project}/_apis/build/builds/{buildId}?api-version={version}</p>
  </blockquote>
  
  <p>How do I add the workitems the right way?</p>
  
  <p>EDIT: I've found an <a href="https://stackoverflow.com/a/34500703/5384879">old post</a> which mentioned a dll in the TFS namespace which has the same capabilities like the call from above. Unluckily, it's not refered in MSDN. Same problem here: no way to add workitems.</p>
  
  <p>To spread the issue and adress it to MS: <a href="https://visualstudio.uservoice.com/forums/330519-team-services/suggestions/15613161-attach-workitem-to-build-via-rest-api" rel="nofollow noreferrer">Patrick has created a post on uservoice</a></p>
  
  <p>UPDATE:</p>
  
  <p>I managed to link a build in the work item. Here is an approach in <a href="/questions/tagged/c%23" class="post-tag" title="show questions tagged &#39;c#&#39;" rel="tag">c#</a>:</p>
  
  <pre><code>var json = new JsonPatchDocument
              {
                  new JsonPatchOperation()
                  {
                      Operation = Operation.Add,
                      Path = "/relations/-",
                      Value = new WorkItemRelation()
                              {
                                  Rel = "ArtifactLink",
                                  Url = build.Uri.ToString()
                              }
                  }
              };
  
  var client = new WebClient { UseDefaultCredentials = true };
  client.Headers.Add(HttpRequestHeader.ContentType, "application/json-patch+json");
  client.UploadData(
      options.CollectionUri + "/_apis/wit/workitems/" + workitemid + "?api-version=1.0",
      "PATCH",
      Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(json)));
  </code></pre>
  
  <p>But there is still no direct binding in the build:
  <a href="https://i.stack.imgur.com/QKr6t.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QKr6t.png" alt="build"></a></p>
  
  <p>And the work items says <code>unknown executable linktype</code>
  <a href="https://i.stack.imgur.com/4l3Eh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4l3Eh.png" alt="work item"></a></p>
  
  <p>According to the message given in the workitem, I assume that I am using the false <a href="https://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.workitemtracking.proxy.workitemrelation.linktypeid(v=vs.100).aspx" rel="nofollow noreferrer">linktype</a>. Does anybody have a reference for me, what types I am able to and should use?</p>
  
  <p>URI UPDATE:
  I am already using the mentioned uri:
  <a href="https://i.stack.imgur.com/ywBHe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ywBHe.png" alt="json"></a></p>
  
  <p><strong>MINOR SOLUTION:</strong></p>
  
  <p>I had to add the name "Build" to the Attributes of the patch. I still does not recognize it in the build itself but for now, I can work with the link as build type.</p>
  
  <pre><code>var json = new JsonPatchDocument
              {
                  new JsonPatchOperation()
                  {
                      Operation = Operation.Add,
                      Path = "/relations/-",
                      Value = new WorkItemRelation()
                              {
                                  Rel = "ArtifactLink",
                                  Url = build.Uri.ToString()
                                  Attributes = new Dictionary&lt;string, object&gt;()
                                  {
                                      { "name", "Build" },
                                      { "comment", build.Result.ToString() }
                                  }
                              }
                  }
              };
  </code></pre>