c# – 如何以编程方式确定是否需要TFS WorkItem字段?
我的项目要求我以编程方式访问我们不管理的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属性. 有没有办法确定是否需要WorkItem字段而不诉诸WIT xml文件?如果没有,有没有办法以编程方式访问WIT xml文件? 解决方法
我需要执行类似的任务,以下是我能弄清楚如何完成它的唯一方法.
正如其他人所提到的,WorkItem验证是在WorkItemType的模板中定义的.根据WorkItem的当前状态甚至当前用户的权限,字段可以具有不同的验证要求. 因此,您需要使用用户的凭据创建/检索WorkItem实例.如果您的应用程序冒充当前用户(即在使用Windows身份验证和模拟的ASP.NET应用程序中),那么您只需使用选项1,即使用TFS API获取WorkItem,而无需模拟. 如果您的应用程序没有模仿用户,那么当您可以使用选项2(使用TFS模拟功能)时,可以在用户上进行调用.这需要在TFS中将“在其他人的行为上做出请求”权限授予应用程序的标识(即在ASP.NET中应用程序池的标识).有关更多信息,请参阅以下链接: 以下代码是有关如何执行选项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 } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |