加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 21:26:14 所属栏目:百科 来源:网络整理
导读:我的项目要求我以编程方式访问我们不管理的TFS服务器,并获取有关WorkItemTypes中字段的实时信息.通过查看WorkItemType的FieldDefinitions集合中的FieldDefinition,我可以获得所需的字段名称和大部分信息. public WitType(WorkItemType type) { this.Fields =
我的项目要求我以编程方式访问我们不管理的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 property = false.

有没有办法确定是否需要WorkItem字段而不诉诸WIT xml文件?如果没有,有没有办法以编程方式访问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
              }
           }
        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读