Using a Kendo Grid Control

Apr 25, 2014 at 3:37 PM
Hi all, thanks for your help so far!

I have an LDAP with over 6K employees

Need to display results of my searches in an Kendo Grid Control. Kenod uses it own paging and sorting logic. Does anyone know if there is a way to make it work with Ling to Ldap?

Here is my code:
@(Html.Kendo().Grid<User>()
    .Name("entiyGrid")
    .Columns(columns =>
        {
            columns.Bound(p => p.FirstName);
            columns.Bound(p => p.LastName);
            columns.Bound(p => p.CountryCode);
            //columns.Bound(p => p.Location);
            //columns.Bound(p => p.PositionTitle);
            //columns.Bound(p => p.Function);
            //columns.Bound(p => p.Phone);
            columns.Bound(p => p.Extension);
            columns.Bound(p => p.Email);
        })
    .Pageable(paging => paging
        .Input(false)
        .Numeric(true)
        .PreviousNext(true)
        .PageSizes(new[] { 10, 25, 50 })
        .Refresh(true))
    .Sortable()
    .Scrollable()
    .Filterable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(10)
        //.Events(events => events.Error("error_handler"))
        .Read(read => read.Action("SearchResultsRead", "EmployeeSearch"))
        )
)
using System;
using System.Collections.Generic;
using LinqToLdap.Mapping;

namespace WS.Framework.Objects.LDAP
{
    [DirectorySchema("ou=Utilisateurs,o=algeco,c=com", ObjectClass = "person")]
    public class User
    {
        [DirectoryAttribute("mail", ReadOnly = true)]
        public string Email { get; set; }

        [DirectoryAttribute("sn", ReadOnly = true)]
        public string LastName { get; set; }

        [DirectoryAttribute("telephonenumber", ReadOnly = true)]
        public string TelephoneNumber { get; set; }

        [DirectoryAttribute("libellesite", ReadOnly = true)]
        public string Department { get; set; }

        [DirectoryAttribute("libellepays", ReadOnly = true)]
        public string Country { get; set; }

        [DirectoryAttribute("dateentree", ReadOnly = true)]
        public string EntryDate { get; set; }

        [DirectoryAttribute("codepays", ReadOnly = true)]
        public string CountryCode { get; set; }

        [DirectoryAttribute("pnptelephone", ReadOnly = true)]
        public string Extension { get; set; }

        [DirectoryAttribute("cn", ReadOnly = true)]
        public string UserName { get; set; }

        [DirectoryAttribute("businesscategory", ReadOnly = true)]
        public string Title { get; set; }

        [DirectoryAttribute("manager2", ReadOnly = true)]
        public string Manager { get; set; }

        [DirectoryAttribute("codesite", ReadOnly = true)]
        public string CodeSite { get; set; }

        [DirectoryAttribute("givenname", ReadOnly = true)]
        public string FirstName { get; set; }

        [DirectoryAttribute("libellepays", ReadOnly = true)]
        public string GlobalEmployeeCode { get; set; }

        [DirectoryAttribute("libellesociete", ReadOnly = true)]
        public string Company { get; set; }

        [DirectoryAttribute("service", ReadOnly = true)]
        public string Area { get; set; }

        [DirectoryAttribute("facsimiletelephonenumber", ReadOnly = true)]
        public string FaxNumber { get; set; }

        public List<UserGroup> UserGroups { get; set; }
        public SafetyIncidentAccess SafetyIncidentAccess { get; set; }
        public HoursWorkedAccess HoursWorkedAccess { get; set; }
    }
}

public ActionResult SearchResultsRead([DataSourceRequest] DataSourceRequest request)
        {
            return Json(GetAllSearchResults().ToDataSourceResult(request));
        }
        
        public IQueryable<User> GetAllSearchResults()
        {
            LdapDirectoryIdentifier ldapDirectoryIdentifier = new LdapDirectoryIdentifier("ldap_server_prod:389");
            NetworkCredential myCredentials = new NetworkCredential("uid=LPassport,ou=services,o=algeco,c=com", "passport");

            using (LdapConnection connection = new LdapConnection(ldapDirectoryIdentifier, myCredentials, AuthType.Basic))
            {
                connection.SessionOptions.ProtocolVersion = 3;
                using (var context = new DirectoryContext(connection))
                {
                    IQueryable<User> searchResultViewModels = context.Query<User>().Where(u => u.FirstName == "Patrick");

                    return searchResultViewModels;

                }
            }
            
        }
Marked as answer by pwensel on 5/6/2014 at 12:12 PM
Coordinator
Apr 28, 2014 at 5:51 PM
Unfortunately server side paging will not work with Kendo grid. LDAP paging does not behave the same way has T-SQL or in-memory collections. You're better off implementing search criteria and returning either all results and performing client side paging or a limited result set.
May 6, 2014 at 7:12 PM
Here is my solution. It uses the Linq to LDAP .Take function, puts them in a list then if it needs to page it removes the earlier ones.
 public ActionResult SearchResultsRead([DataSourceRequest] DataSourceRequest request)
        {

            EmployeeSearchAdditionalData employeeSearchAdditionalData = new EmployeeSearchAdditionalData();

            TryUpdateModel(employeeSearchAdditionalData);

            string domain = ConfigurationManager.AppSettings["LDAPDomain"];
            string serviceUser = ConfigurationManager.AppSettings["ServiceUser"];
            string servicePassword = ConfigurationManager.AppSettings["ServicePassword"];

            LdapDirectoryIdentifier ldapDirectoryIdentifier = new LdapDirectoryIdentifier(domain);
            NetworkCredential myCredentials = new NetworkCredential(serviceUser, servicePassword);

            LdapConnection connection = new LdapConnection(ldapDirectoryIdentifier, myCredentials, AuthType.Basic);
            connection.SessionOptions.ProtocolVersion = 3;
            DirectoryContext context = new DirectoryContext(connection);

            var orders = context.Query<User>().Where(u => u.FirstName.Contains(employeeSearchAdditionalData.Keyword));

            var total = orders.Count();

            orders = orders.ApplyOrdersSorting(request.Groups, request.Sorts);

            var x = orders.ApplyOrdersPaging(request.Page, request.PageSize, total);

            var result = new DataSourceResult()
            {
                Data = x,
                Total = total
            };

            return Json(result);

        }
public static List<User> ApplyOrdersPaging(this IQueryable<User> data, int page, int pageSize, int total)
        {
            List<User> users = new List<User>();

            if (pageSize > 0 && page > 0)
            {
 __               users = data.Take(page * pageSize).ToList();
                if (page > 1)
                {
                    users.RemoveRange(0, ((page-1)*pageSize));
                }__
            }

            return users;
        }