internal static ADGroup CreateUniqueRoleGroup(IRecipientSession session, ADObjectId dom, ADObjectId containerId, string groupNameOrig, int groupId, string groupDescription, OrganizationId orgId)
        {
            string groupSam = InitializeExchangeUniversalGroups.FindUniqueSamAccountName(session, dom, groupNameOrig);

            return(InitializeExchangeUniversalGroups.CreateUniqueRoleGroup(session, dom, containerId, groupNameOrig, groupId, groupDescription, groupSam, null, orgId));
        }
        private ADGroup CreateGroup(OrganizationId orgId, ADObjectId usgContainerId, string groupName, int groupId, Guid wkGuid, string groupDescription, GroupTypeFlags groupType, List <ADObjectId> manageBy)
        {
            ADGroup      adgroup      = null;
            DNWithBinary dnwithBinary = DirectoryCommon.FindWellKnownObjectEntry(this.configurationUnit.OtherWellKnownObjects, wkGuid);

            if (null != dnwithBinary)
            {
                ADObjectId adobjectId = new ADObjectId(dnwithBinary.DistinguishedName);
                if (adobjectId.IsDeleted)
                {
                    base.WriteError(new InvalidWKObjectException(dnwithBinary.ToString(), orgId.ConfigurationUnit.DistinguishedName), ErrorCategory.InvalidData, null);
                }
                ADRecipient adrecipient = this.orgDomainRecipientSession.Read(adobjectId);
                if (adrecipient == null)
                {
                    base.WriteError(new InvalidWKObjectException(dnwithBinary.ToString(), orgId.ConfigurationUnit.DistinguishedName), ErrorCategory.InvalidData, null);
                }
                base.LogReadObject(adrecipient);
                if (adrecipient.RecipientType != RecipientType.Group)
                {
                    base.WriteError(new InvalidWKObjectTargetException(wkGuid.ToString(), orgId.ConfigurationUnit.ToString(), adgroup.Id.DistinguishedName, groupType.ToString()), ErrorCategory.InvalidData, null);
                }
                adgroup = (adrecipient as ADGroup);
                InitializeExchangeUniversalGroups.UpgradeRoleGroupLocalization(adgroup, groupId, groupDescription, this.orgDomainRecipientSession);
                if ((adgroup.GroupType & groupType) != groupType)
                {
                    base.WriteVerbose(Strings.InfoChangingGroupType(adgroup.Id.DistinguishedName, groupType.ToString()));
                    adgroup.GroupType            = groupType;
                    adgroup.RecipientTypeDetails = RecipientTypeDetails.RoleGroup;
                    this.orgDomainRecipientSession.Save(adgroup);
                    base.LogWriteObject(adgroup);
                }
                else
                {
                    base.WriteVerbose(Strings.InfoGroupAlreadyPresent(adgroup.Id.DistinguishedName));
                }
                return(adgroup);
            }
            ADGroup adgroup2 = null;

            try
            {
                string groupSam = groupName + "{" + Guid.NewGuid().ToString("N") + "}";
                adgroup2     = InitializeExchangeUniversalGroups.CreateUniqueRoleGroup(this.orgDomainRecipientSession, orgId.OrganizationalUnit.DomainId, usgContainerId, groupName, groupId, groupDescription, groupSam, manageBy, orgId);
                dnwithBinary = this.CreateWKGuid(adgroup2.Id, wkGuid);
            }
            finally
            {
                if (adgroup2 == null && dnwithBinary != null)
                {
                    this.configurationUnit.OtherWellKnownObjects.Remove(dnwithBinary);
                    this.configurationSession.Save(this.configurationUnit);
                    base.LogWriteObject(this.configurationUnit);
                }
                else if (adgroup2 != null && dnwithBinary == null)
                {
                    this.orgDomainRecipientSession.Delete(adgroup2);
                    base.LogWriteObject(adgroup2);
                    adgroup2 = null;
                }
            }
            return(adgroup2);
        }
        private ADGroup CreateGroup(ADOrganizationalUnit usgContainer, string groupName, int groupId, Guid wkGuid, string groupDescription, GroupTypeFlags groupType, bool createAsRoleGroup)
        {
            ADRecipient  adrecipient  = base.ResolveExchangeGroupGuid <ADRecipient>(wkGuid);
            DNWithBinary dnwithBinary = null;

            if (adrecipient != null)
            {
                base.LogReadObject(adrecipient);
                if (adrecipient.RecipientType != RecipientType.Group)
                {
                    base.WriteError(new InvalidWKObjectTargetException(wkGuid.ToString(), "CN=Microsoft Exchange,CN=Services," + this.configurationSession.ConfigurationNamingContext.DistinguishedName, adrecipient.Id.DistinguishedName, groupType.ToString()), ErrorCategory.NotSpecified, null);
                }
                ADGroup adgroup = adrecipient as ADGroup;
                base.LogReadObject(adgroup);
                if ((adgroup.GroupType & groupType) != groupType)
                {
                    base.WriteError(new InvalidWKObjectTargetException(wkGuid.ToString(), "CN=Microsoft Exchange,CN=Services," + this.configurationSession.ConfigurationNamingContext.DistinguishedName, adgroup.Id.DistinguishedName, groupType.ToString()), ErrorCategory.NotSpecified, null);
                }
                if (createAsRoleGroup && adgroup.RecipientTypeDetails != RecipientTypeDetails.RoleGroup)
                {
                    base.WriteError(new InvalidWKObjectTargetException(wkGuid.ToString(), "CN=Microsoft Exchange,CN=Services," + this.configurationSession.ConfigurationNamingContext.DistinguishedName, adgroup.Id.DistinguishedName, RecipientTypeDetails.RoleGroup.ToString()), ErrorCategory.NotSpecified, null);
                }
                base.WriteVerbose(Strings.InfoGroupAlreadyPresent(adgroup.Id.DistinguishedName));
                dnwithBinary = DirectoryCommon.FindWellKnownObjectEntry(this.exchangeConfigContainer.OtherWellKnownObjects, wkGuid);
                if (dnwithBinary == null)
                {
                    dnwithBinary = this.CreateWKGuid(this.exchangeConfigContainer, adgroup.Id, wkGuid);
                }
                if (createAsRoleGroup)
                {
                    InitializeExchangeUniversalGroups.UpgradeRoleGroupLocalization(adgroup, groupId, groupDescription, this.rootDomainRecipientSession);
                }
                return(adgroup);
            }
            ADContainer adcontainer = this.exchangeConfigContainer;

            dnwithBinary = DirectoryCommon.FindWellKnownObjectEntry(adcontainer.OtherWellKnownObjects, wkGuid);
            if (dnwithBinary == null)
            {
                adcontainer  = this.configContainer;
                dnwithBinary = DirectoryCommon.FindWellKnownObjectEntry(adcontainer.OtherWellKnownObjects, wkGuid);
            }
            if (dnwithBinary != null)
            {
                base.WriteError(new InvalidWKObjectException(dnwithBinary.ToString(), adcontainer.DistinguishedName), ErrorCategory.NotSpecified, null);
            }
            ADGroup adgroup2 = null;

            try
            {
                if (createAsRoleGroup)
                {
                    adgroup2 = InitializeExchangeUniversalGroups.CreateUniqueRoleGroup(this.rootDomainRecipientSession, this.rootDomain.Id, usgContainer.Id, groupName, groupId, groupDescription, OrganizationId.ForestWideOrgId);
                }
                else
                {
                    adgroup2 = InitializeExchangeUniversalGroups.CreateUniqueChildSG(this.rootDomainRecipientSession, this.rootDomain.Id, usgContainer.Id, groupName, groupDescription, groupType, OrganizationId.ForestWideOrgId);
                }
                dnwithBinary = this.CreateWKGuid(this.exchangeConfigContainer, adgroup2.Id, wkGuid);
            }
            finally
            {
                if (adgroup2 == null && dnwithBinary != null)
                {
                    this.exchangeConfigContainer.OtherWellKnownObjects.Remove(dnwithBinary);
                    this.domainConfigurationSession.Save(this.exchangeConfigContainer);
                    base.LogWriteObject(this.exchangeConfigContainer);
                }
                else if (adgroup2 != null && dnwithBinary == null)
                {
                    this.rootDomainRecipientSession.Delete(adgroup2);
                    base.LogWriteObject(adgroup2);
                    adgroup2 = null;
                }
            }
            return(adgroup2);
        }