【问题标题】:validate method returns fields, which are not requiredvalidate 方法返回不需要的字段
【发布时间】:2017-03-03 11:11:47
【问题描述】:

当我创建一个新工作项时,我将其设置为:

WorkItem workitem = new WorkiItem(workItemType);

然后我只需直接验证即可获得所有必填字段,因此我知道在保存之前需要填写哪些字段。

ArrayList requiredFields = workitem.Validate();

但在我的 requiredFields 中,有一个字段,在字段定义中未标记为必填字段。例如,有一个字段“Assigned To”,它不是必填字段,因此我可以在 TFS-Webapplication 内部创建一个工作项,而无需填写此字段。那么为什么在验证时它会被放入 requiredField 列表中呢?如果它没有经过验证,我不想保存它。

【问题讨论】:

标签: c# validation tfs tfs-workitem


【解决方案1】:

WorkItem 验证在 WorkItemType 的模板中定义。根据WorkItem的当前状态(option1)或当前用户的权限(option2),字段可以有不同的验证要求以下代码是这两个选项的示例,供您参考:

 // Set the following variables accordingly
        string workItemTypeName = "Bug";
        string teamProjectName = "My Project";
        string usernameToImpersonate = "XXX";
        string tfsTeamProjectCollectionUrl = "http://xxx:8080/tfs/defaultcollection";

    // 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
          }
       }
    }

【讨论】:

  • 这似乎很有帮助,但是您从哪里获得 workItemId 变量?
  • 以workItemId 为例,了解如何获取现有的workItem。如果您的应用程序支持编辑现有工作项,那么您何时需要获取现有工作项才能查看需要哪些字段。如果您只是总是创建新的工作项,那么是的,您不需要 if (workItemId.HasValue)
  • 更多详情请参考本教程blog.joergbattermann.com/2016/05/05/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 2017-09-28
相关资源
最近更新 更多