validate方法返回不需要的字段

时间:2016-10-21 08:16:02

标签: c# validation tfs tfs-workitem

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

WorkItem workitem = new WorkiItem(workItemType);

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

ArrayList requiredFields = workitem.Validate();

但是在我的requiredFields中,有一个字段,它们没有被标记为fielddefinition中的必填字段。例如,有一个字段" Assigned To",这不是必填字段,因此我可以在TFS-Web应用程序内部创建工作项而不填写此字段。那么为什么在验证时将其放入requiredField列表?如果没有经过验证,我不想保存它。

1 个答案:

答案 0 :(得分:0)

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

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