You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用TFS API无法将变更集链接到现有工作项的问题求助

问题

我尝试借助Azure DevOps Services(及TFS)的.NET客户端库,在C#中实现将变更集链接到现有TFS工作项的功能,使用的代码如下:

public void CreateChangesetLinkAsync(int workitemID, int ChangesetId, string comment)
{
    string changesetUrl = $"vstfs:///_versionControl/Changeset/{ChangesetId}";

    try
    {
        // Define the update operations using a JsonPatchDocument
        JsonPatchDocument patchDocument = new JsonPatchDocument();
        patchDocument.Add(new JsonPatchOperation()
        {
            Operation = Operation.Add,
            Path = "/relations/-",
            Value = new
            {
                rel = "ArtifactLink",
                url =$"{changesetUrl}",
                attributes = new {name = $"{comment}", LinkType = "Hyperlink" }
            }
        });

        // Send the update request
        WorkItemTrackingHttpClient witClient = tfs.GetClient<WorkItemTrackingHttpClient>();
        witClient.UpdateWorkItemAsync(patchDocument, workitemID).Wait();
    }
    catch (Exception)
    {
        throw;
    }
}

但每次执行都会抛出相同错误:

VssServiceException: Unrecognized Resource link: Link Name: Merge changeset 377894 (workitem 174339) from Key2Dev322000 to TrunkSP Link Uri: vstfs:///_versionControl/Changeset/377986

解决方法

问题出在链接类型的配置错误,具体修正点如下:

  • 你在attributes里错误设置了LinkType = "Hyperlink",变更集属于版本控制工件,不需要指定该属性,服务端会自动识别vstfs:///_versionControl/Changeset/xxx格式的URL对应的链接类型。
  • 工件链接的备注字段应为comment,而非代码里的name,这是Azure DevOps/TFS的标准字段名。

修正后的代码:

public void CreateChangesetLinkAsync(int workitemID, int changesetId, string comment)
{
    string changesetUrl = $"vstfs:///_versionControl/Changeset/{changesetId}";

    try
    {
        JsonPatchDocument patchDocument = new JsonPatchDocument();
        patchDocument.Add(new JsonPatchOperation()
        {
            Operation = Operation.Add,
            Path = "/relations/-",
            Value = new
            {
                rel = "ArtifactLink",
                url = changesetUrl,
                attributes = new 
                { 
                    comment = comment
                }
            }
        });

        WorkItemTrackingHttpClient witClient = tfs.GetClient<WorkItemTrackingHttpClient>();
        witClient.UpdateWorkItemAsync(patchDocument, workitemID).Wait();
    }
    catch (Exception)
    {
        throw;
    }
}

内容的提问来源于stack exchange,提问作者Tom Wigley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 05:50:20