2017-05-30 40 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"}; 
相關問題