![](/img/trans.png)
[英]Creating workitem in Azure DevOps using REST API - Status 404 not found
[英]Create an Issue (WorkItem) in Azure DevOps with Attachments using a REST API (C#)
我需要在 DevOps 中創建一個帶有附件的問題類型的 WorkItem,今天我正在創建但沒有附件,我想知道如何通過 json 添加附件並執行 POST,我的代碼執行如下:理解、令牌和驗證在別處)
代碼在 C #:
string requestUrl = "https://dev.azure.com/"+organizacao+"/"+project+"/_apis/wit/workitems/$Chamado Issue?api-version=6.0"; //Verificar sempre a versão da api
//Montando o json/Atribuindo valores aos campos
try
{
// if (ExisteDevOps()=="0")
//{ //Criando Lista de campos
List<Object> Fields = new List<Object> { };
//Titulo
Fields.Add(new
{
op = "add",
path = "/fields/System.Title",
value = this.Titulo
});
//Descricao
Fields.Add(new
{
op = "add",
path = "/fields/System.Description",
value = this.Descricao
});
//Nome de usuario
Fields.Add(new
{
op = "add",
path = "/fields/Custom.a0cfd3d4-4ce1-4426-b360-261f00ea6a3b",
value = this.Nome_Usuario
});
//Email
Fields.Add(new
{
op = "add",
path = "/fields/Custom.1b77b0ed-15c3-4e9f-8a66-ac83b9375d65",
value = this.email
});
//SetorSolcitante
Fields.Add(new
{
op = "add",
path = "/fields/Custom.SetorSolicitante",
value = this.setor
});
//Sistema
Fields.Add(new
{
op = "add",
path = "/fields/Custom.Sistema",
value = this.sistema
});
檢查下面的代碼片段,這是一個 .net 示例:
[ClientSampleMethod]
public WorkItem AddAttachment()
{
int id = Convert.ToInt32(Context.GetValue<WorkItem>("$newWorkItem3").Id);
string filePath = ClientSampleHelpers.GetSampleTextFile();
VssConnection connection = Context.Connection;
WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();
// upload attachment to store and get a reference to that file
AttachmentReference attachmentReference = workItemTrackingClient.CreateAttachmentAsync(filePath).Result;
JsonPatchDocument patchDocument = new JsonPatchDocument();
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Test,
Path = "/rev",
Value = "1"
}
);
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/System.History",
Value = "Adding the necessary spec"
}
);
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/relations/-",
Value = new
{
rel = "AttachedFile",
url = attachmentReference.Url,
attributes = new { comment = "VanDelay Industries - Spec" }
}
}
);
WorkItem result = workItemTrackingClient.UpdateWorkItemAsync(patchDocument, id).Result;
return result;
}
您可以在以下鏈接中獲得更多示例:
如果要使用 REST api,可以參考附件 - 創建api:
POST https://dev.azure.com/{organization}/{project}/_apis/wit/attachments?fileName={fileName}&uploadType={uploadType}&areaPath={areaPath}&api-version=6.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.