protected override void OnBeforeSave()
 {
     base.OnBeforeSave();
     if (base.IsNew)
     {
         bool   flag;
         byte[] nodeBefore = FolderTreeData.GetOrdinalValueOfFirstMatchingNode(base.MailboxSession, CalendarGroup.FindLastGroupOrdinalSortOrder, (IStorePropertyBag row) => CalendarGroup.IsFolderTreeData(row) && CalendarGroup.IsCalendarSection(row) && CalendarGroup.IsCalendarGroup(row), CalendarGroup.CalendarInfoProperties, out flag);
         if (flag && !FolderTreeData.MyFoldersClassId.Equals(this.GroupClassId))
         {
             using (CalendarGroup calendarGroup = CalendarGroup.CreateMyCalendarsGroup(base.MailboxSession))
             {
                 nodeBefore = calendarGroup.NodeOrdinal;
             }
         }
         base.SetNodeOrdinalInternal(nodeBefore, null);
     }
 }
        public static CalendarGroup Bind(MailboxSession session, Guid groupClassId)
        {
            Util.ThrowOnNullArgument(session, "session");
            Util.ThrowOnNullArgument(groupClassId, "groupClassId");
            if (groupClassId.Equals(Guid.Empty))
            {
                throw new ArgumentException("Invalid GroupClassId", "groupClassId");
            }
            CalendarGroup calendarGroup = null;
            bool          flag          = true;
            bool          flag2         = false;
            CalendarGroup result;

            using (Folder folder = Folder.Bind(session, DefaultFolderType.CommonViews))
            {
                using (QueryResult queryResult = folder.ItemQuery(ItemQueryType.Associated, null, CalendarGroup.CalendarGroupViewSortOrder, CalendarGroup.CalendarInfoProperties))
                {
                    queryResult.SeekToCondition(SeekReference.OriginBeginning, CalendarGroup.CalendarSectionFilter);
                    while (flag)
                    {
                        IStorePropertyBag[] propertyBags = queryResult.GetPropertyBags(10000);
                        if (propertyBags.Length == 0)
                        {
                            break;
                        }
                        for (int i = 0; i < propertyBags.Length; i++)
                        {
                            IStorePropertyBag storePropertyBag = propertyBags[i];
                            if (!CalendarGroup.IsCalendarSection(storePropertyBag))
                            {
                                flag = false;
                                break;
                            }
                            if (CalendarGroup.IsFolderTreeData(storePropertyBag))
                            {
                                if (!flag2 && CalendarGroup.IsCalendarGroup(storePropertyBag, groupClassId))
                                {
                                    flag2 = true;
                                    VersionedId storeId = (VersionedId)storePropertyBag.TryGetProperty(ItemSchema.Id);
                                    calendarGroup = CalendarGroup.Bind(session, storeId, null);
                                }
                                if (flag2)
                                {
                                    calendarGroup.LoadChildNodesCollection(propertyBags, i);
                                    break;
                                }
                            }
                        }
                    }
                    if (flag2)
                    {
                        result = calendarGroup;
                    }
                    else if (FolderTreeData.MyFoldersClassId.Equals(groupClassId))
                    {
                        result = CalendarGroup.CreateMyCalendarsGroup(session);
                    }
                    else
                    {
                        if (!FolderTreeData.OtherFoldersClassId.Equals(groupClassId))
                        {
                            throw new ObjectNotFoundException(ServerStrings.ExItemNotFound);
                        }
                        result = CalendarGroup.InternalCreateGroup(session, CalendarGroupType.OtherCalendars);
                    }
                }
            }
            return(result);
        }