Search code examples
tfsodata

TFS OData workitem get last insert id


I'm using TFS OData service to create work item which is work fine. now i need to get id of work item and insert into database for later reference can anyone guide me direction for this ?

here is what i get as a response.

   <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xml:base="https://tfsodata.visualstudio.com/DefaultCollection/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:etag="W/&quot;datetime'2013-08-31T10%3A23%3A21.1439028%2B00%3A00'&quot;" xmlns="http://www.w3.org/2005/Atom">
  <id>https://tfsodata.visualstudio.com/DefaultCollection/WorkItems(0)</id>
  <title type="text">my test work item</title>
  <summary type="text">test work item created by isuru http://www.google.com</summary>
  <updated>2013-08-31T10:23:21Z</updated>
  <author>
    <name />
  </author>
  <link rel="edit" title="WorkItem" href="WorkItems(0)" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Attachments" type="application/atom+xml;type=feed" title="Attachments" href="WorkItems(0)/Attachments" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Links" type="application/atom+xml;type=feed" title="Links" href="WorkItems(0)/Links" />
  <category term="Microsoft.Samples.DPE.ODataTFS.Model.Entities.WorkItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
  <content type="application/xml">
    <m:properties>
      <d:Id m:type="Edm.Int32">0</d:Id>
      <d:Project>My Website</d:Project>
      <d:Type>Product Backlog Item</d:Type>
      <d:WebEditorUrl m:null="true" />
      <d:AreaPath m:null="true" />
      <d:IterationPath m:null="true" />
      <d:Revision m:type="Edm.Int32">0</d:Revision>
      <d:Priority m:null="true" />
      <d:Severity m:null="true" />
      <d:StackRank m:type="Edm.Double">0</d:StackRank>
      <d:AssignedTo m:null="true" />
      <d:CreatedDate m:type="Edm.DateTime">2013-08-31T10:23:21.1419032+00:00</d:CreatedDate>
      <d:CreatedBy m:null="true" />
      <d:ChangedDate m:type="Edm.DateTime">2013-08-31T10:23:21.1439028+00:00</d:ChangedDate>
      <d:ChangedBy m:null="true" />
      <d:ResolvedBy m:null="true" />
      <d:Title>my test work item</d:Title>
      <d:State m:null="true" />
      <d:Reason m:null="true" />
      <d:CompletedWork m:type="Edm.Double">0</d:CompletedWork>
      <d:RemainingWork m:type="Edm.Double">0</d:RemainingWork>
      <d:Description>test work item created by isuru http://www.google.com</d:Description>
      <d:ReproSteps m:null="true" />
      <d:FoundInBuild m:null="true" />
      <d:IntegratedInBuild m:null="true" />
      <d:AttachedFileCount m:type="Edm.Int32">0</d:AttachedFileCount>
      <d:HyperLinkCount m:type="Edm.Int32">0</d:HyperLinkCount>
      <d:RelatedLinkCount m:type="Edm.Int32">0</d:RelatedLinkCount>
      <d:Risk m:null="true" />
      <d:StoryPoints m:type="Edm.Double">0</d:StoryPoints>
      <d:OriginalEstimate m:type="Edm.Double">0</d:OriginalEstimate>
      <d:BacklogPriority m:type="Edm.Double">0</d:BacklogPriority>
      <d:BusinessValue m:type="Edm.Int32">0</d:BusinessValue>
      <d:Effort m:type="Edm.Double">0</d:Effort>
      <d:Blocked m:null="true" />
      <d:Size m:type="Edm.Double">0</d:Size>
    </m:properties>
  </content>
</entry>

Solution

  • What I did was adding a Guid to a random field that you are not using and query for that Guid afterwards.

            wi.Title = "New Work Item";
            wi.FoundInBuild = Guid.NewGuid().ToString();
            wi.Type = "Bug";
            wi.Project = "TestProject";
    
            context.AddToWorkItems(wi);
            context.SaveChanges();
            Debug.WriteLine(String.Format("Work item created"));
    
            var createdWi = context.WorkItems.Where( d => d.FoundInBuild == wi.FoundInBuild);