title: How to change workflow state of the newly created TFS work item through API?
body: <p>I'm creating a work item migration application from "something" to TFS 2013, and I want to have the TFS work items to be in corresponding workflow states as in source system. For instance, if the source work item is in "Closed" state, I want it to be in "Done" state in TFS.</p>
  
  <p>I have followed advises in <a href="http://geekswithblogs.net/TarunArora/archive/2012/10/27/tfsapichangeworkitemcreatedandchangeddatetohistoricdates.aspx" rel="nofollow">this article</a>, which suggests to set <code>BypassRules</code> property of the <code>WorkItemStore</code> object to <code>true</code> in order to be able to set <code>CreatedDate</code> field. I suppose, the same applies to changing the workflow state, as it also requires bypassing rules.</p>
  
  <p>So, I tried the following:</p>
  
  <pre><code>// obtain collection and authenticate towards it
  var collection = new TfsTeamProjectCollection(new Uri(_tfsUrl), cred);
  collection.Authenticate();
  
  // get the work item store object
  var store = new WorkItemStore(collection, WorkItemStoreFlags.BypassRules);
  
  // creating the work item
  var workItem = new WorkItem(store.Projects[_tfsProjectName].WorkItemTypes["Product Backlog Item"]);
  
  // setting some standard fields
  workItem.Title = "some name";
  workItem.Description = "some description";
  
  // validating the work item
  if (workItem.Validate().Count &gt; 0)
  {
     // throw validation rules violated
  }
  
  // saving the work item
  workItem.Save();    
  </code></pre>
  
  <p>As you can see, this sample doesn't violate any validation rules, and <code>workItem.Validate().Count</code> returns <code>0</code>. But the call to <code>workItem.Save()</code> throws the following exception:</p>
  
  <blockquote>
    <p>Additional information: TF26212: Team Foundation Server could not save
    your changes. There may be problems with the work item type
    definition. Try again or contact your Team Foundation Server
    administrator.</p>
  </blockquote>
  
  <p>I double checked that <code>BypassRules</code> is set to <code>true</code> right before the call to <code>Save()</code> method. Besides, the <code>workItem.IsValid</code> is also <code>true</code>. </p>
  
  <p>The interesting fact is that if I change the way I obtain the <code>WorkItemStore</code> object, from</p>
  
  <pre><code>var store = new WorkItemStore(collection, WorkItemStoreFlags.BypassRules);
  </code></pre>
  
  <p>to </p>
  
  <pre><code>var store = collection.GetService&lt;WorkItemStore&gt;();
  </code></pre>
  
  <p>it can save without any problem! But in this case I don't know how to set <code>BypassRules</code> to <code>true</code>. This property is read-only when the <code>WorkItemStore</code> object is created, and I get validation errors if I try to set workflow step to something other than "New".</p>
  
  <p>So, my basic question is: <strong>how to create work items in TFS through API and be able to change <code>State</code> field in this newly created item?</strong></p>