PivotViewerを使用してActive Directoryユーザーを参照する

最近、SilverlightのすばらしいコントロールであるPivotViewerがリリースされました。 Active Directoryユーザーを表示するために使用してみましょう。 理解を容易にするために、Pivo​​tの作成者が提供する完成したプロジェクトを使用します。



動作するには、次のものが必要です。





行こう:



ダウンロードしたPivotJitServerプロジェクトをVisual Studio 2010で開きます。 CollectionFactoriesプロジェクトを見つけ、そこに新しいクラスを追加し、ActiveDirectoryFactoryと呼び、CollectionFactoryBaseから継承します。



using System;

using System.Collections. Generic ;

using System.Linq;

using System.Text;

using PivotServerTools;



namespace CollectionFactories

{

public class ActiveDirectoryFactory : CollectionFactoryBase

{

public ActiveDirectoryFactory()

{

this .Summary = "ActiveDirectory collection" ;

}

}

}




* This source code was highlighted with Source Code Highlighter .








次に、System.DirectoryServicesアセンブリへの参照をCollectionFactoriesプロジェクトに追加し、「using System.DirectoryServices;」をクラスに追加します。



必要なパラメーターを含む補助クラスActiveDirectoryUserを作成しましょう。 便宜上、クラスで辞書を使用して、パラメーターと値のバインディングコード(内部のコメント)を格納します。



public class ActiveDirectoryUser

{

// , ActiveDirectory

public ActiveDirectoryUser(SearchResult entry)

{

_Parameters = new Dictionary< String , Object>();



// AD

foreach ( string propertyName in entry.Properties.PropertyNames)

{

_Parameters.Add(propertyName, entry.Properties[propertyName][0]);

}

}



private Dictionary< String , Object> _Parameters;

public Dictionary< String , Object> Parameters

{

get { return _Parameters; }

}

}




* This source code was highlighted with Source Code Highlighter .








CollectionFactoryBaseクラスに要素のコレクションを作成するために、Pivo​​tServerTools.Collection(表示する要素のコレクション)を返すMakeCollectionメソッドがあります。 コレクションをその中の要素で満たすために変更する必要があります。



public override Collection MakeCollection(CollectionRequestContext context)

{

Collection collection = new Collection(); //

collection.Name = "ActiveDirectory database" ; //

List <ActiveDirectoryUser> users = GetObjects( "LDAP://servad" , "(&(objectClass=user)(objectCategory=person))" ); // AD



//

foreach (ActiveDirectoryUser user in users)

{

ItemImage itemImage = null ; // , AD, http



// , , , String.Empty

collection.AddItem(

user.Parameters.ContainsKey( "displayname" ) ? user.Parameters[ "displayname" ].ToString() : user.Parameters[ "samaccountname" ].ToString(),

null , null , itemImage,

user.Parameters.ContainsKey( "samaccountname" ) ? new Facet( "Account Name" , FacetType.Text, user.Parameters[ "samaccountname" ].ToString()) : new Facet( "Account Name" , FacetType.Text, String .Empty),

user.Parameters.ContainsKey( "lastlogon" ) ? new Facet( "Last Logon" , FacetType. DateTime , DateTime .FromFileTime(( long )user.Parameters[ "lastlogon" ])) : new Facet( "Last Logon" , FacetType. DateTime , new DateTime ()),

user.Parameters.ContainsKey( "company" ) ? new Facet( "Company" , FacetType.Text, user.Parameters[ "company" ].ToString()) : new Facet( "Company" , String .Empty),

user.Parameters.ContainsKey( "department" ) ? new Facet( "Department" , FacetType.Text, user.Parameters[ "department" ].ToString()) : new Facet( "Department" , String .Empty),

user.Parameters.ContainsKey( "title" ) ? new Facet( "Title" , FacetType.Text, user.Parameters[ "title" ].ToString()) : new Facet( "Title" , String .Empty)

);

}



collection.SetFacetDisplay( "Account Name" , false , true , false ); // ,

collection.SetFacetFormat( "Last Logon" , "dd-MM-yyyy hh:mm:ss" ); //



return collection;

}



* This source code was highlighted with Source Code Highlighter .








SetFacetDisplayを使用すると、指定されたパラメーターをフィルターに表示するのではなく、ピボットで選択したオブジェクトのプロパティに表示できます。

SetFacetFormatを使用すると、日付形式を設定できます 。 この場合、最後のユーザーログインの日付形式が設定されます。



ActiveDirectoryでオブジェクトを検索するコード



private List <ActiveDirectoryUser> GetObjects( String domainpath, String filter)

{

List <ActiveDirectoryUser> allUsers = new List <ActiveDirectoryUser>();



DirectoryEntry searchRoot = new DirectoryEntry(domainpath);

DirectorySearcher search = new DirectorySearcher(searchRoot);

search.PageSize = 1000;

search.Filter = filter;

search.PropertiesToLoad.Add( "displayName" );

search.PropertiesToLoad.Add( "samaccountname" );

search.PropertiesToLoad.Add( "department" );

search.PropertiesToLoad.Add( "company" );

search.PropertiesToLoad.Add( "lastlogon" );

search.PropertiesToLoad.Add( "title" );



SearchResult result;

SearchResultCollection resultCol = search.FindAll();

if (resultCol != null )

{

for ( int counter = 0; counter < resultCol.Count; counter++)

{

result = resultCol[counter];

ActiveDirectoryUser adObject = new ActiveDirectoryUser(result);



allUsers.Add(adObject);

}

}

return allUsers;

}



* This source code was highlighted with Source Code Highlighter .






ここで重要なパラメーターはPageSizeです;設定されていない場合、検索は1000個以下の結果を返します。



すべて準備完了です。 Webプロジェクトを起動し、「PivotViewerコントロールを使用してSilverlightアプリケーションでサンプルを表示するにはここをクリック」ページで選択します。 サンプルコレクションとして、ActiveDirectoryFactory.csを選択します。その後、データの入力が開始され、会社、部門、職位でソートできるユーザーのリスト全体が表示されます。



個人的には、会社から役職に至るまで、ユーザーのパラメーターに多くのタイプミスが見つかりました。 dsqueryではなく、シンプルで便利な表示。



小さな画面で表示するのはあまり便利ではないので、 ここに写真へのリンクがあります



All Articles