ブログ@kaorun55

HoloLensやKinectなどのDepthセンサーを中心に書いています。

TFS Advent Calendarの21日目・TFSのユーザー一覧を取得する #tfsug

この記事はTFS Advent Calendarの21日目の記事です。
サイトはこちら:http://atnd.org/events/22819

プロジェクト一式はこちらです。

移行作業などでAPIを使う場合に、ユーザーも合わせて移行したい場合があります。
そのときに、移行元と、TFSのユーザーを結びつけるために、TFSのユーザー一覧を取得する必要があります。
今回は、TFSのユーザー一覧を取得する方法を解説します。
f:id:kaorun55:20111221005450p:image

使い方

取得するリストの名前を入れれば、そこにいるユーザーが帰ってきます。

コレクションのメンバー

「プロジェクト コレクション管理者」一覧を取得します。
f:id:kaorun55:20111221005451p:image


メンバーはこんな感じです
f:id:kaorun55:20111221005453p:image

コード
// コレクションのユーザー一覧
var users = tfs.GetUserList( @"[DefaultCollection]\プロジェクト コレクション管理者" );
foreach ( var user in users ) {
    Console.WriteLine( user );
}

プロジェクトのメンバー

「プロジェクト管理者」一覧を取得します。
f:id:kaorun55:20111221005454p:image


メンバーはこんな感じです
f:id:kaorun55:20111221005452p:image

コード
// プロジェクトのユーザー一覧
users = tfs.GetUserList( @"[TFS_API_SAMPLE]\プロジェクト管理者" );
foreach ( var user in users ) {
    Console.WriteLine( user );
}

TfsClient

ユーザー名の一覧を取得する

factorValue が取得したいグループの名前になります。IGroupSecurityServiceを使って、ユーザー名のリストを取得しています。

public List<string> GetUserList( string factorValue )
{
    Identity SIDS = groupSecurityService.ReadIdentity( SearchFactor.AccountName,
        factorValue, QueryMembership.Expanded );
    Identity[] UserId = groupSecurityService.ReadIdentities( SearchFactor.Sid,
                                                SIDS.Members, QueryMembership.None );
    List<string> users = new List<string>();
    foreach ( Identity user in UserId ) {
        users.Add( user.AccountName );
    }

    return users;
}

IGroupSecurityServiceを取得する

GetTfsProjectの中で行っています。

groupSecurityService = (IGroupSecurityService)teamProjectCollection.GetService( typeof( IGroupSecurityService ) );

まとめ

このようにコレクション、プロジェクトの権限ごとににユーザー一覧が取得できます。
このユーザーを作業項目の担当に割り振るなどして使うことができます。