public DataTable SearchDirectoryWithCustomSettings(string nameFilter, ParentType dirType, bool includeDeleted)
        {
            IDirectoryCollection dirsWithCustomSettings = new IDirectoryCollection();
            IDirectoryCollection allBrands, allAgencies;
            string parentTypeName = "Invalid";
            int    parentBrandID = 0, brandID = 0, agencyID = 0; string parentBrandName = string.Empty, brandName = string.Empty, agencyName = string.Empty;

            this.GetAllBrandsAndAgencies(out allBrands, out allAgencies, true, includeDeleted);

            dirsWithCustomSettings.AddRange(this.GetChildren(includeDeleted).FindAll(dir => (!dir.InheritParentFields || dir.IsNotificationActive)));
            dirsWithCustomSettings.AddRange(allBrands.FindAll(dir => (!dir.InheritParentFields || !dir.InheritParentNotification)));
            dirsWithCustomSettings.AddRange(allAgencies.FindAll(dir => (!dir.InheritParentFields || !dir.InheritParentNotification)));

            DataTable dtResult = new DataTable();

            dtResult.Columns.Add("ID", typeof(int));
            dtResult.Columns.Add("Name", typeof(string));
            dtResult.Columns.Add("ParentType", typeof(string));
            dtResult.Columns.Add("PBID", typeof(int));
            dtResult.Columns.Add("BID", typeof(int));
            dtResult.Columns.Add("AGID", typeof(int));
            dtResult.Columns.Add("IsNoticationActive", typeof(bool));
            dtResult.Columns.Add("InheritsNotification", typeof(bool));
            dtResult.Columns.Add("InheritParentFields", typeof(bool));
            dtResult.Columns.Add("PBName", typeof(string));
            dtResult.Columns.Add("BRName", typeof(string));
            dtResult.Columns.Add("AGName", typeof(string));

            var result = (from dir in dirsWithCustomSettings
                          where Utility.CheckDirType(dir, dirType, out parentTypeName, out parentBrandID, out brandID, out agencyID, out parentBrandName, out brandName, out agencyName) &&
                          (string.IsNullOrEmpty(nameFilter) ? true : dir.Name.ToUpper().Contains(nameFilter.ToUpper())) &&
                          dir.DoesUserHasWritePermission
                          select new {
                ID = dir.ID,
                Name = dir.Name,
                ParentType = parentTypeName,
                PBID = parentBrandID,
                BID = brandID,
                AGID = agencyID,
                IsNoticationActive = dir.IsNotificationActive,
                InheritsNotification = dir.InheritParentNotification,
                InheritParentFields = dir.InheritParentFields,
                PBName = parentBrandName,
                BRName = brandName,
                AGName = agencyName
            }).ToList();


            foreach (var item in result)
            {
                dtResult.Rows.Add(item.ID, item.Name, item.ParentType, item.PBID, item.BID, item.AGID, item.IsNoticationActive, item.InheritsNotification, item.InheritParentFields, item.PBName, item.BRName, item.AGName);
            }

            return(dtResult);
        }
        public IDirectoryCollection GetChildren(bool includeDeleted, bool applySecurityTrimming = true)
        {
            string viewFields = string.Concat(
                "<FieldRef Name='" + Constants.List_ID_Field + "' />",
                "<FieldRef Name='" + Constants.GAD_ID_Field + "' />",
                "<FieldRef Name='" + Constants.Notification_InheritParentNotification_Field + "' />",
                "<FieldRef Name='" + Constants.Field_InheritParentFields_Field + "' />",
                "<FieldRef Name='" + Constants.Notification_IsNotificationActive_Field + "' />",
                "<FieldRef Name='" + Constants.Parent_Brand_Name_Field + "' />",
                "<FieldRef Name='" + Constants.IsDeleted_Field + "' />"
                );

            string query = string.Empty;

            if (!includeDeleted)
            {
                query = "<Where><Eq><FieldRef Name='" + Constants.IsDeleted_Field + "' /><Value Type='Boolean'>0</Value></Eq></Where>";
            }

            SPListItemCollection spItems = GetItemByQuery(Constants.Parent_Brand_Url, viewFields, query);

            IDirectoryCollection parentBrands = new IDirectoryCollection();

            foreach (SPListItem item in spItems)
            {
                int    id    = Convert.ToInt32(item[Constants.List_ID_Field]);
                int    gadID = Convert.ToInt32(item[Constants.GAD_ID_Field]);
                string name  = Convert.ToString(item[Constants.Parent_Brand_Name_Field]);
                bool   inheritParentField   = Convert.ToBoolean(item[Constants.Field_InheritParentFields_Field] ?? "false");
                bool   inheritParentNoti    = Convert.ToBoolean(item[Constants.Notification_InheritParentNotification_Field] ?? "false");
                bool   isaAtiveNotification = Convert.ToBoolean(item[Constants.Notification_IsNotificationActive_Field] ?? "false");
                bool   isDeleted            = Convert.ToBoolean(item[Constants.IsDeleted_Field] ?? "false");
                parentBrands.Add(new ParentBrand(id, name, gadID, this)
                {
                    InheritParentFields = inheritParentField, InheritParentNotification = inheritParentNoti, IsNotificationActive = isaAtiveNotification, IsDeleted = isDeleted
                });
            }

            if (applySecurityTrimming)
            {
                IDirectoryCollection parentBrandsSecured = new IDirectoryCollection();
                parentBrandsSecured.AddRange(parentBrands.FindAll(pb => pb.DoesUserHasPermission));
                return(parentBrandsSecured);
            }
            else
            {
                return(parentBrands);
            }
        }
        public void GetAllBrandsAndAgencies(out IDirectoryCollection allBrands, out IDirectoryCollection allAgencies, bool getAgencies, bool includeDeleted, bool applySecurityTrimming = true)
        {
            allBrands   = new IDirectoryCollection();
            allAgencies = new IDirectoryCollection();

            foreach (ParentBrand pb in this.GetChildren(includeDeleted, applySecurityTrimming))
            {
                IDirectoryCollection pbBrands = pb.GetChildren(includeDeleted, applySecurityTrimming);
                allBrands.AddRange(pbBrands);

                if (getAgencies)
                {
                    foreach (Brand b in pbBrands)
                    {
                        IDirectoryCollection bAgencies = b.GetChildren(includeDeleted, applySecurityTrimming);
                        allAgencies.AddRange(bAgencies);
                    }
                }
            }
        }
        public DataTable SearchDirectoryWithCustomSettings(string nameFilter, ParentType dirType,bool includeDeleted)
        {
            IDirectoryCollection dirsWithCustomSettings = new IDirectoryCollection();
            IDirectoryCollection allBrands, allAgencies;
            string parentTypeName="Invalid";
            int parentBrandID = 0, brandID = 0, agencyID = 0; string parentBrandName = string.Empty, brandName = string.Empty, agencyName = string.Empty;
            this.GetAllBrandsAndAgencies(out allBrands, out allAgencies, true,includeDeleted);

            dirsWithCustomSettings.AddRange(this.GetChildren(includeDeleted).FindAll(dir => (!dir.InheritParentFields || dir.IsNotificationActive)));
            dirsWithCustomSettings.AddRange(allBrands.FindAll(dir => (!dir.InheritParentFields || !dir.InheritParentNotification)));
            dirsWithCustomSettings.AddRange(allAgencies.FindAll(dir => (!dir.InheritParentFields || !dir.InheritParentNotification)));

            DataTable dtResult = new DataTable();
            dtResult.Columns.Add("ID", typeof(int));
            dtResult.Columns.Add("Name", typeof(string));
            dtResult.Columns.Add("ParentType", typeof(string));
            dtResult.Columns.Add("PBID", typeof(int));
            dtResult.Columns.Add("BID", typeof(int));
            dtResult.Columns.Add("AGID", typeof(int));
            dtResult.Columns.Add("IsNoticationActive", typeof(bool));
            dtResult.Columns.Add("InheritsNotification", typeof(bool));
            dtResult.Columns.Add("InheritParentFields", typeof(bool));
            dtResult.Columns.Add("PBName", typeof(string));
            dtResult.Columns.Add("BRName", typeof(string));
            dtResult.Columns.Add("AGName", typeof(string));

            var result = (from dir in dirsWithCustomSettings
                          where Utility.CheckDirType(dir, dirType, out parentTypeName, out parentBrandID, out brandID, out agencyID, out parentBrandName ,out brandName,out agencyName) &&
                               (string.IsNullOrEmpty(nameFilter) ? true : dir.Name.ToUpper().Contains(nameFilter.ToUpper())) &&
                               dir.DoesUserHasWritePermission
                          select new {
                              ID = dir.ID,
                              Name = dir.Name,
                              ParentType = parentTypeName,
                              PBID = parentBrandID,
                              BID = brandID,
                              AGID = agencyID,
                              IsNoticationActive = dir.IsNotificationActive,
                              InheritsNotification = dir.InheritParentNotification,
                              InheritParentFields=dir.InheritParentFields,
                              PBName = parentBrandName,
                              BRName = brandName,
                              AGName = agencyName
                          }).ToList();

            foreach (var item in result)
            {
                dtResult.Rows.Add(item.ID,item.Name,item.ParentType,item.PBID,item.BID,item.AGID,item.IsNoticationActive,item.InheritsNotification,item.InheritParentFields,item.PBName,item.BRName,item.AGName);
            }

            return dtResult;
        }
        public IDirectoryCollection GetChildren(bool includeDeleted, bool applySecurityTrimming = true)
        {
            string viewFields = string.Concat(
                                   "<FieldRef Name='" + Constants.List_ID_Field + "' />",
                                   "<FieldRef Name='" + Constants.GAD_ID_Field + "' />",
                                   "<FieldRef Name='" + Constants.Notification_InheritParentNotification_Field + "' />",
                                   "<FieldRef Name='" + Constants.Field_InheritParentFields_Field + "' />",
                                   "<FieldRef Name='" + Constants.Notification_IsNotificationActive_Field + "' />",
                                   "<FieldRef Name='" + Constants.Parent_Brand_Name_Field + "' />",
                                   "<FieldRef Name='" + Constants.IsDeleted_Field + "' />"
                                   );

            string query=string.Empty;

            if (!includeDeleted)
                query = "<Where><Eq><FieldRef Name='" + Constants.IsDeleted_Field + "' /><Value Type='Boolean'>0</Value></Eq></Where>";

            SPListItemCollection spItems = GetItemByQuery(Constants.Parent_Brand_Url, viewFields, query);

            IDirectoryCollection parentBrands = new IDirectoryCollection();

            foreach (SPListItem item in spItems)
            {
                int id = Convert.ToInt32(item[Constants.List_ID_Field]);
                int gadID = Convert.ToInt32(item[Constants.GAD_ID_Field]);
                string name = Convert.ToString(item[Constants.Parent_Brand_Name_Field]);
                bool inheritParentField = Convert.ToBoolean(item[Constants.Field_InheritParentFields_Field] ?? "false");
                bool inheritParentNoti = Convert.ToBoolean(item[Constants.Notification_InheritParentNotification_Field] ?? "false");
                bool isaAtiveNotification = Convert.ToBoolean(item[Constants.Notification_IsNotificationActive_Field] ?? "false");
                bool isDeleted = Convert.ToBoolean(item[Constants.IsDeleted_Field] ?? "false");
                parentBrands.Add(new ParentBrand(id, name, gadID,this) { InheritParentFields = inheritParentField, InheritParentNotification = inheritParentNoti, IsNotificationActive = isaAtiveNotification,IsDeleted=isDeleted });
            }

            if (applySecurityTrimming)
            {
                IDirectoryCollection parentBrandsSecured = new IDirectoryCollection();
                parentBrandsSecured.AddRange(parentBrands.FindAll(pb => pb.DoesUserHasPermission));
                return parentBrandsSecured;
            }
            else
            {
                return parentBrands;
            }
        }
        public void GetAllBrandsAndAgencies(out IDirectoryCollection allBrands, out IDirectoryCollection allAgencies, bool getAgencies, bool includeDeleted, bool applySecurityTrimming = true)
        {
            allBrands = new IDirectoryCollection();
            allAgencies = new IDirectoryCollection();

            foreach (ParentBrand pb in this.GetChildren(includeDeleted, applySecurityTrimming))
            {
                IDirectoryCollection pbBrands = pb.GetChildren(includeDeleted, applySecurityTrimming);
                allBrands.AddRange(pbBrands);

                if (getAgencies)
                {
                    foreach (Brand b in pbBrands)
                    {
                        IDirectoryCollection bAgencies = b.GetChildren(includeDeleted, applySecurityTrimming);
                        allAgencies.AddRange(bAgencies);
                    }
                }

            }
        }