簡體   English   中英

如何以編程方式確定是否需要TFS WorkItem字段?

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

我的項目要求我以編程方式訪問我們不管理的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
              }
           }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM