无法在Eval中验证TFS工作项字段

时间:2016-08-30 20:12:47

标签: c# html asp.net tfs wiql

我有一个TFS工作项字段似乎存在于大多数工作项目中,但有时并不存在。

我尝试使用WorkItemCollection在列表视图中绑定这些工作项,除非我尝试绑定该字段,否则一切正常。

字段本身在查询选择中指定,不会出错。然而,获得该字段而不在某些工作项上抛出错误的唯一方法是(在.cs中)::

          foreach(WorkItem w in queryResults)
          {
            if (w.Fields.Contains("Symptom"))
            {
              w.Fields["Symptom"].Value.ToString();//show
            }
          }

由于这些工作项目处于只读模式,我不能强制写入值等等。(除非我返回一个与工作项配对的Dictionnary,但我不认为那会是最好的解决方案......)

我尝试做的是Listview ItemTemplate,类似这样::

                <div class="details">
                    <%# Server.HtmlEncode(Eval("Fields.Contains(\"Symptom\")? Fields[\"Symptom\"].Value : \"\";").ToString())%>
                </div>

但是我得到了&#39;包含&#39;不是有效的&#39; Fields&#39;属性(因为它是一种收集方法) - 我坚持使用eval

如何正确评估和显示我的症状字段?

2 个答案:

答案 0 :(得分:0)

如果您使用TFS 2015或VSTS,则可以使用REST API来获取症状字段。 API看起来像:

GET http(s)://{instance}/DefaultCollection/_apis/wit/workitems?ids=xx&fields=Microsoft.VSTS.CMMI.Symptom&api-version=1.0

或.net Api:

using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Client;
using System;

namespace TestCaseProject
{
    class Program
    {
        static void Main(string[] args)
        {
            var tfs =
         TfsTeamProjectCollectionFactory.GetTeamProjectCollection(
             new Uri("http://tfsserver:8080/tfs/CollectionName"));
            var service = tfs.GetService<WorkItemStore>();

            var wi = service.GetWorkItem(id);


            foreach (Field field in wi.Fields)
            {
                Console.WriteLine("{0}: {1}", field.Name, field.Value);
            }


        }
    }
}

答案 1 :(得分:0)

Use OnItemDataBound

Nothing seemed to work directly on the page so I moved away from that approach and went with attempting to solve my problem in the codebehind, since I was already capable of sorting it out on that front.

I removed my sourceObject from my page and instead had the codebehind programmatically databind my listview. (this step is not necessary)

TFSListView.DataSource = SearchHandler.SearchTFS(searchstring);
TFSListView.DataBind();

Then I used OnItemDatabound event to be able to manage each item being bound, I check if that annoying symptom field exist for the dataitem being bound and fill up the appropriate control if it does (all this replaces one eval line, so I'm still a bit annoyed but it didn't seem to be possible directly in the aspx page)

protected void TFSListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
  ListViewDataItem myitem = (ListViewDataItem)e.Item; 

  if (e.Item.ItemType == ListViewItemType.DataItem) 
  {
    Control divDetails = e.Item.FindControl("divLsvDetails");
    WorkItem myWI = myitem.DataItem as WorkItem;

    if (myWI != null && divDetails != null)
    {          
      if (myWI.Fields.Contains("Symptom"))
      {            
        ((HtmlGenericControl)divDetails).InnerHtml = myWI["Symptom"].ToString();
      }
    }
  }
}