Scope One Level

Jul 4, 2013 at 10:59 AM
Is there an easy way to apply a scope to the retrieved values.

I would like to get a list of OU's from a predefined parent OU. This works but the query returns all the OU even the ones in the Child OU's

Basic scopes (LDAP)=> base / one level / subtree

Parent OU
  • Child 1
    -- SubChild 1 => Do not want this
    -- SubChild 2 => Do not want this
  • Child 2
  • Child 3
I just need one level. What is the best way to do this?

Thanks
Coordinator
Jul 12, 2013 at 10:05 PM
Hi bmeer,

Sorry for the late response. I committed some changes a few weeks ago and this feature was in that change set. I haven't had an official release of the new changes yet. Below is an example of how to do this. I made a new collection type called EntrySetCollection<T> which creates a query containing the parent EntryObject and the root. You can see the use of the Scope property of the EntryCollectionPropertyAttribute on the Ous property. This allows for exactly what you are wanting to do.
    [DirectoryType("organizationalunit")]
    public class OU : EntryObject
    {
        [DirectoryProperty("name", true)]
        public string Name { get; set; }

        [EntryCollectionProperty(MatchingRule = MatchingRuleType.Children, Scope = SearchScope.OneLevel)]
        public EntrySetCollection<OU> Ous
        {
            get
            {
                return ((IEntryWithRelationships)this).RelationshipManager.GetEntrySetCollection<OU>("Ous");
            }
        }

        [EntryCollectionProperty(MatchingRule = MatchingRuleType.Children)]
        public EntrySetCollection<User> Users
        {
            get
            {
                return ((IEntryWithRelationships)this).RelationshipManager.GetEntrySetCollection<User>("Users");
            }
        }

        [EntryCollectionProperty(MatchingRule = MatchingRuleType.Children)]
        public EntrySetCollection<Group> Groups
        {
            get
            {
                return ((IEntryWithRelationships)this).RelationshipManager.GetEntrySetCollection<Group>("Groups");
            }
        }
    }

// Example usage
using (var context = new DirectoryContextMock())
{
    var ou = context.OrganizationalUnits.Single(u => u.Name == "TestOU");
    var result = ou.Ous.Last(u => u.Name != null);
    Assert.AreEqual(result.Name, "Test2");
}

Thanks, and I hope this helps.

Stephen Baker
Marked as answer by sbaker on 10/7/2013 at 9:42 AM
Jul 15, 2013 at 10:39 AM
Thanks for the response.

That was exacly what I needed.

Oh and thanks again for the great library.

Bert