private ExchangePrincipal CreateExchangePrincipal(IGenericADUser user, ADObjectId mdb, IMailboxLocation mailboxLocation, RemotingOptions remotingOptions, bool asArchive, Guid?aggregatedMailboxGuid, IDatabaseLocationProvider databaseLocationProvider, bool isContentIndexing = false)
        {
            ExchangePrincipalBuilder exchangePrincipalBuilder = ((remotingOptions & RemotingOptions.AllowHybridAccess) == RemotingOptions.AllowHybridAccess) ? new RemoteUserMailboxPrincipalBuilder(user) : this.GetExchangePrincipalBuilder(user);

            exchangePrincipalBuilder.SetRemotingOptions(remotingOptions);
            exchangePrincipalBuilder.SetDatabaseLocationProvider(databaseLocationProvider);
            if (!mdb.IsNullOrEmpty())
            {
                exchangePrincipalBuilder.SetSelectedMailboxDatabase(mdb.ObjectGuid);
            }
            if (mailboxLocation != null)
            {
                exchangePrincipalBuilder.SetSelectedMailboxLocation(mailboxLocation);
            }
            if (asArchive)
            {
                exchangePrincipalBuilder.SelectArchiveMailbox();
            }
            exchangePrincipalBuilder.BypassRecipientTypeValidation(isContentIndexing);
            if (aggregatedMailboxGuid != null && aggregatedMailboxGuid != Guid.Empty)
            {
                exchangePrincipalBuilder.SelectMailbox(aggregatedMailboxGuid.Value);
            }
            return(exchangePrincipalBuilder.Build());
        }
        private ExchangePrincipal CreateExchangePrincipal(string displayName, IMailboxLocation mailboxLocation, RemotingOptions remotingOptions, string mailboxLegacyDN, Guid mailboxGuid, Guid mdbGuid, string primarySmtpAddress, ADObjectId id, OrganizationId orgId, IEnumerable <CultureInfo> preferredCultures, bool bypassRemoteCheck, RecipientType recipientType, RecipientTypeDetails recipientTypeDetails, SecurityIdentifier masterAccountSid = null, bool isArchive = false, Guid?aggregatedMailboxGuid = null)
        {
            ADObjectId     adobjectId = new ADObjectId(mdbGuid);
            IGenericADUser recipient  = new GenericADUser
            {
                MailboxDatabase        = (isArchive ? null : adobjectId),
                ArchiveDatabase        = (isArchive ? adobjectId : null),
                LegacyDn               = mailboxLegacyDN,
                OrganizationId         = orgId,
                DisplayName            = displayName,
                PrimarySmtpAddress     = new SmtpAddress(primarySmtpAddress),
                MailboxGuid            = (isArchive ? Guid.Empty : mailboxGuid),
                ArchiveGuid            = (isArchive ? mailboxGuid : Guid.Empty),
                Languages              = preferredCultures,
                RecipientType          = recipientType,
                RecipientTypeDetails   = recipientTypeDetails,
                ObjectId               = id,
                MasterAccountSid       = masterAccountSid,
                AggregatedMailboxGuids = ((aggregatedMailboxGuid != null) ? new Guid[]
                {
                    aggregatedMailboxGuid.Value
                } : Array <Guid> .Empty)
            };
            ExchangePrincipalBuilder exchangePrincipalBuilder = this.GetExchangePrincipalBuilder(recipient);

            exchangePrincipalBuilder.SetRemotingOptions(remotingOptions);
            exchangePrincipalBuilder.BypassRecipientTypeValidation(true);
            if (mailboxLocation != null)
            {
                exchangePrincipalBuilder.SetSelectedMailboxLocation(mailboxLocation);
            }
            if (isArchive)
            {
                exchangePrincipalBuilder.SelectArchiveMailbox();
            }
            if (aggregatedMailboxGuid != null && aggregatedMailboxGuid != Guid.Empty)
            {
                exchangePrincipalBuilder.SelectMailbox(aggregatedMailboxGuid.Value);
            }
            return(exchangePrincipalBuilder.Build());
        }