c# - 如何以编程方式确定是否需要 TFS WorkItem 字段?

标签 c#

我的项目要求我以编程方式访问我们不管理的 TFS 服务器,并获取有关 WorkItemTypes 中字段的实时信息。通过查看 WorkItemType 的 FieldDefinitions 集合中的 FieldDefinition,我可以获得字段名称和我需要的大部分信息。

    public WitType(WorkItemType type)
    {
        this.Fields = new List<string>();

        foreach (FieldDefinition f in type.FieldDefinitions)
        {
            Fields.Add(f.Name);
        }
    }

缺少的一件事是 IsRequired 属性。我需要能够判断是否需要一个字段。 我试过运行工作项故事查询

WorkItemCollection workItemCollection = workItemStore.Query
foreach (WorkItem workItem in workItemCollection)
foreach (Field field in workItem.Fields)
{
     textBox1.Text += field.Name + " is required? " +  field.IsRequired.ToString();                 
}

然后检查 WorkItem 的 Fields 集合中 Field 项的 IsRequired 属性。 唯一的问题是,对于给定的工作项类型,一个工作项说标题是必需的,然后下一个工作项将具有 IsRequired 属性 = false。

有没有一种方法可以在不求助于 WIT xml 文件的情况下确定是否需要 WorkItem 字段?如果没有,有没有办法以编程方式访问 WIT xml 文件?

最佳答案

我需要执行类似的任务,以下是我能弄清楚如何完成它的唯一方法。

正如其他人所提到的,WorkItem 验证是在 WorkItemType 的模板中定义的。根据 WorkItem 的当前状态甚至当前用户的权限,字段可以有不同的验证要求。

因此,您需要使用用户的凭据创建/检索 WorkItem 实例。如果您的应用程序正在模拟当前用户(即在使用 Windows 身份验证和模拟的 ASP.NET 应用程序中),那么您只需使用选项 1,即使用 TFS API 获取 WorkItem,而不进行模拟。

如果您的应用程序不模拟用户,那么您可以使用选项 2,在其中使用 TFS 模拟功能,以用户的行为进行调用。这需要在 TFS 中向应用程序的身份(即在 ASP.NET 中应用程序池的身份)授予“根据其他人的行为发出请求”权限。有关详细信息,请参见以下链接: http://blogs.microsoft.co.il/blogs/shair/archive/2010/08/23/tfs-api-part-29-tfs-impersonation.aspx

以下代码是关于如何执行选项 1 和选项 2 的示例。

        // Set the following variables accordingly
        string workItemTypeName = "Bug";
        string teamProjectName = "My Project";
        string usernameToImpersonate = "joesmith";
        string tfsTeamProjectCollectionUrl = "http://mydomain.com:8080/tfs/ProjectCollectionName";

        // OPTION 1: no impersonation.
        // Get an instance to TFS using the current thread's identity.
        // NOTE: The current thread's identity needs to have the "" permision or else you will receive
        //       a runtime SOAP exception: "Access Denied: [username] needs the following permission(s) to perform this action: Make requests on behalf of others"
        TfsTeamProjectCollection tfs = new TfsTeamProjectCollection( new Uri( tfsTeamProjectCollectionUrl ) );
        IIdentityManagementService identityManagementService = tfs.GetService<IIdentityManagementService>();

        // OPTION 2: impersonation.  Remove the following two lines of code if you don't need to impersonate.
        // Get an instance to TFS impersonating the specified user.
        // NOTE: This is not needed if the current thread's identity is that of the user 
        //       needed to impersonate. Simple use the ablve TfsTeamProjectCollection instance
        TeamFoundationIdentity identity = identityManagementService.ReadIdentity( IdentitySearchFactor.AccountName, usernameToImpersonate, MembershipQuery.None, ReadIdentityOptions.None );
        tfs = new TfsTeamProjectCollection( tfs.Uri, identity.Descriptor );

        WorkItem workItem = null;
        WorkItemStore store = tfs.GetService<WorkItemStore>();

        // Determine if we are creating a new WorkItem or loading an existing WorkItem.
        if( workItemId.HasValue ) {
           workItem = store.GetWorkItem( workItemId.Value );
        }
        else {
           Project project = store.Projects[ teamProjectName ];
           WorkItemType workItemType = project.WorkItemTypes[ workItemTypeName ];
           workItem = new WorkItem( workItemType );
        }

        if( workItem != null ) {

           foreach( Field field in workItem.Fields ) {
              if( field.IsRequired ) {
                 // TODO
              }
           }
        }

关于c# - 如何以编程方式确定是否需要 TFS WorkItem 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8220352/

相关文章:

c# - 我如何防止使用 C# 进行屏幕录制

c# - 从 C# 更改 for 循环中的 arduino 变量

c# - 最小起订量 - 使用 DbEntityEntry 更新

c# - WIF 通过 AJAX 到一个单独的域

c# - XPathSelectElement 与后代

c# - 如何在 ASP.Net MVC 5 应用程序中从浏览器获取 URL

C# 确定泛型类型

javascript - 客户端自定义验证未执行

c# - Selenium 查找指定元素内的元素

c# - 是否可以将 EF Core 查询与 LINQ 和动态字符串查询混合编写​​?