Thursday, November 10, 2011

LINQ To LDAP: More Control

Currently paging and sorting controls will automatically be created for the by the framework. That's nice for most situations, but sometimes you need a little more control.

I added a WithControls extension method that allows you to specify as many additional DirectoryControls as necessary.

List<User> list = context.Query<User>()
    .WithControls(new DirectoryControl[]
                    {
                        new PageResultRequestControl(100)
                            {
                                IsCritical = true
                            },
                        new SortRequestControl(new[]{new SortKey("cn", null, true)})
                            {
                                IsCritical = true
                            },
                        new ShowDeletedControl
                            {
                                IsCritical = true
                            }
                    })
    .ToList();

If you try to perform an OrderBy or ToPage operation while specifying your own via WithControls then an InvalidOperationException will be thrown.

One cool thing here is behind the scenes I still check if you're requesting a page so you can cast the resulting list as a LdapPage and get the cookie for the next page. And that's about it.

4 comments:

  1. I noticed that you specified multiple directory controls in your search request, specifically PageResultRequestControl and SortRequestControl. When i try to do the same the search completes successfully for the first page of results. However moving to the next page causes an error: "the server does not support the control".
    If i don't add the SortRequestControl and just do paging then everything works fine, and vice versa.
    Is there a trick to getting a paged search working with both these controls added to the search request?

    ReplyDelete
  2. This seems to be a bug. If you use the filter produced from the LdapPage for a subsequent request, you'll get this error because the QueryTranslator is appending the object class twice. That is invalidating the page request since it's technically a different filter. Try using IgnoreOC in the second request and let me know if that works. I will create a task for this and should have a fix soon. Thanks for catching this!

    ReplyDelete
  3. Great work really , please i have the same problem of "dfranzmann" , is this bug fixed

    I tried also your workaround but not working

    thanks

    ReplyDelete
  4. i found a solution to this problem , you must use the same DirectoryContext and don't create new one in the second request

    ReplyDelete