📜 ⬆️ ⬇️

Browse Active Directory Users with PivotViewer

Most recently released a wonderful control for Silverlight - PivotViewer. Let's try to use it to view Active Directory users. For convenience of understanding, we will use the finished project provided by the creators of Pivot.

To work we need:


Go:

Open the downloaded PivotJitServer project in Visual Studio 2010. Find the CollectionFactories project and add a new class there, call it ActiveDirectoryFactory and inherit it from 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 .


Next, add a link to the System.DirectoryServices assembly to the CollectionFactories project and add “using System.DirectoryServices;” to our class.

Let's create an auxiliary class ActiveDirectoryUser, which will contain the parameters we need. For convenience, the class uses Dictionary for storing parameter-value code bindings (comments inside):

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 .


To create a collection of items in the CollectionFactoryBase class, there is a MakeCollection method that returns a PivotServerTools.Collection (a collection of items to display). We need to change it in order to fill the collection with our elements.

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 allows you not to display the specified parameter in the filter, but to show it in the properties with the selected object in Pivot.
SetFacetFormat allows you to set the date format. In our case, the format is set to the date of the last user login.

And code to search for objects in 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 .

Here the important parameter is PageSize, if it is not set, the search will return no more than 1000 results.

All is ready. We launch the web project and select it on the page “Click here to view the samples in a Silverlight application using the PivotViewer control”. As sample collection, select ActiveDirectoryFactory.cs, after which filling will begin with data and you will see the entire list of users with the ability to sort by company, department, position ...

Personally, I immediately found a bunch of typos in the parameters of users, ranging from company to position. Simple and convenient viewing, instead of dsquery.

Since the display on the small screen is not very convenient, here is a link to the pictures

Source: https://habr.com/ru/post/99198/


All Articles