How can I programmatically determine if a TFS WorkItem field is required?

My project requires me to programmatically access TFS servers that we don’t administer, and to receive real-time information about the fields in WorkItemTypes. I can get the field names and most of the information I need if I look at the FieldDefinition in the WorkItemType FieldDefinitions collection.

public WitType(WorkItemType type) { this.Fields = new List<string>(); foreach (FieldDefinition f in type.FieldDefinitions) { Fields.Add(f.Name); } } 

One IsRequired property is missing. I need to know if a field is required. I tried to run a work item history query

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

and then checking the IsRequired property of the Field element in the Field Worktem collection. The only problem is that for a given work item of type one work item says that a header is needed, then the next work item will have the property IsRequired = false.

Is there a way to determine if a WorkItem field is required without resorting to a WIT xml file? If not, is there a way to programmatically access the WIT xml file?

+4
source share
1 answer

I needed to perform a similar task, and the only thing I could understand how to do this was as follows:

As others have mentioned, the WorkItem check is defined in the WorkItemType template. Fields can have different validation requirements based on the current state of the WorkItem and even the current user permissions.

Therefore, you need to create / get an instance of WorkItem using user credentials. If your application impersonates the current user (i.e., in an ASP.NET application using Windows authentication and impersonation), you can simply use Option 1, where you use the TFS API to get the WorkItem without impersonation.

If you are an application, this does not impersonate the user when you can use Option 2, where you use the TFS impersonation function to make calls to user behavior. This requires granting the permission β€œMake requests for the behavior of others” in TFS to the application identifier (that is, in ASP.NET the application pool identifier). See the following link for more information: http://blogs.microsoft.co.il/blogs/shair/archive/2010/08/23/tfs-api-part-29-tfs-impersonation.aspx

The following code is an example of how to make options 1 and option 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 identity. // NOTE: The current thread 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 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 } } } 
+1
source

Source: https://habr.com/ru/post/1382402/


All Articles