2017-05-30 77 views
-1

我是使用TFS工作的新手。下面是我试图执行的代码检索项目的代码,我得到如下错误的代码。请建议我,如果我失去了一些东西。我正在使用TFS onpremise。VssPropertyValidationException:无法在TFS 2017中使用REST API获取工作项目

{ “的$ id”: “1”, “的InnerException”:空, “消息”: “你必须在请求主体传递查询对象。” “的typeName”:“微软.VisualStudio.Services.Common.VssPropertyValidationException,Microsoft.VisualStudio.Services.Common,Version = 14.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a“, ”typeKey“:”VssPropertyValidationException“,”errorCode“:0,”eventId“ :3000}

var resturl = "tfs Server URL/tfs/collectionname/_apis/wit/wiql?api-version=1.0" 

var query = "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Feature' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc"; 

$.ajax({ 
    "url": resturl, 
    "type": "POST", 
    "Content-type": "application/json", 
    body: JSON.stringify(query), 
    headers: { 
     "Accept": "application/json" 
    }, 
    success: function (data) { 
     alert('success'); 
     }, 
    error: function (error) { 
     alert('failure'); 
     alert(error.statusText); 
     alert(error.responseJSON.message); 
     } 
    }); 

[编辑]:我能够从下面的代码的REST API检索的项目列表。

var resturl = "tfs Server URL/tfs/collectionname/_apis/projects?stateFilter=All" 

$.ajax({ 
      "url": resturl, 
      "type": "GET", 
      "Content-type": "application/json",    
      headers: { 
       "Accept": "application/json" 
      }, 
      success: function (data) {     
       alert('success'); 
      }, 
      error: function (error) { 
       alert('failure'); 
       alert(error.statusText); 
       alert(error.responseJSON.message); 
      } 
     }); 
+0

发出定额。从[this]找到解决方案(https://social.msdn.microsoft.com/Forums/vstudio/en-US/20b2e061-ce0b-4d42-812c-b44065745d05/tfs-restful-api-wiql-workitem-query- lanaguage?forum = tfsgeneral)论坛。非常有用的一个。 – dia

回答

1

您使用了错误的格式,该var query字符串格式不是JSON格式。这就是为什么你得到了错误"You must pass a query object in the body of the request."

您可以参考这个官方教程Work item query language

POST https://{instance}/DefaultCollection/[{project}/]_apis/wit/wiql?api-version={version} 

Content-type: application/json 

{ 
    "query": string 
} 

所以你需要使用bleow格式

var query = {"query": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Feature' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc"}; 
相关问题