public static ADUser GetLocalOrganizationMailboxByCapability(OrganizationId orgId, OrganizationCapability capability, bool allowRehoming = true)
        {
            Util.ThrowOnNullArgument(orgId, "orgId");
            EnumValidator.ThrowIfInvalid <OrganizationCapability>(capability, "capability");
            ExTraceGlobals.StorageTracer.TraceDebug <OrganizationId, OrganizationCapability>(0L, "GetLocalOrganizationMailboxByCapability orgId='{0}', capability='{1}'", orgId, capability);
            IRecipientSession session = OrganizationMailbox.GetSession(orgId, allowRehoming);
            List <ADUser>     organizationMailboxesByCapability = OrganizationMailbox.GetOrganizationMailboxesByCapability(session, capability);

            if (organizationMailboxesByCapability == null || organizationMailboxesByCapability.Count == 0)
            {
                ExTraceGlobals.StorageTracer.TraceDebug <OrganizationId, OrganizationCapability>(0L, "GetLocalOrganizationMailboxByCapability orgId='{0}', capability='{1}' -- No org mailboxes found", orgId, capability);
                return(null);
            }
            Server localServer = LocalServerCache.LocalServer;

            if (localServer == null)
            {
                ExTraceGlobals.StorageTracer.TraceDebug <OrganizationId, OrganizationCapability>(0L, "GetLocalOrganizationMailboxByCapability orgId='{0}', capability='{1}' -- No local server found", orgId, capability);
                return(null);
            }
            Database[] databases = localServer.GetDatabases();
            if (databases == null || databases.Length == 0)
            {
                ExTraceGlobals.StorageTracer.TraceDebug <OrganizationId, OrganizationCapability>(0L, "GetLocalOrganizationMailboxByCapability orgId='{0}', capability='{1}' -- No local databases found", orgId, capability);
                return(null);
            }
            foreach (ADUser aduser in organizationMailboxesByCapability)
            {
                ExTraceGlobals.StorageTracer.TraceDebug <OrganizationId, OrganizationCapability, string>(0L, "GetLocalOrganizationMailboxByCapability orgId='{0}', capability='{1}' -- Processing mbox='{2}'", orgId, capability, aduser.DistinguishedName);
                foreach (Database database in databases)
                {
                    if (aduser.Database.Equals(database.Id))
                    {
                        ExTraceGlobals.StorageTracer.TraceDebug <OrganizationId, OrganizationCapability, string>(0L, "GetLocalOrganizationMailboxByCapability orgId='{0}', capability='{1}' -- returning mbox='{2}'", orgId, capability, aduser.DistinguishedName);
                        return(aduser);
                    }
                }
            }
            return(null);
        }
 public static ADUser[] FindByOrganizationId(OrganizationId organizationId, OrganizationCapability capability)
 {
     return(OrganizationMailbox.GetOrganizationMailboxesByCapability(OrganizationMailbox.GetSession(organizationId, true), capability).ToArray());
 }
 public static bool TryFindByOrganizationId(OrganizationId organizationId, ADObjectId siteId, OrganizationCapability capability, out List <ADUser> siteUsers, out List <ADUser> allUsers)
 {
     ExAssert.RetailAssert(!OrganizationMailbox.IsMultiTenantEnvironment(), "TryFindByOrganizationId (site-based) should not be used in Datacenter Multitenant environment");
     OrganizationMailbox.GetOrganizationMailboxesByCapability(OrganizationMailbox.GetSession(organizationId, true), capability, siteId, out siteUsers, out allUsers);
     return(allUsers.Count > 0);
 }
        public static bool IsOrganizationAnchoredOnLocalServer(OrganizationId orgId, bool allowRehoming = true)
        {
            Util.ThrowOnNullArgument(orgId, "orgId");
            ExTraceGlobals.StorageTracer.TraceDebug <OrganizationId>(0L, "IsOrganizationAnchoredOnLocalServer orgId='{0}'", orgId);
            OrganizationCapability organizationCapability            = OrganizationCapability.Management;
            List <ADUser>          organizationMailboxesByCapability = OrganizationMailbox.GetOrganizationMailboxesByCapability(OrganizationMailbox.GetSession(orgId, allowRehoming), organizationCapability);

            if (organizationMailboxesByCapability == null || organizationMailboxesByCapability.Count == 0)
            {
                ExTraceGlobals.StorageTracer.TraceDebug <OrganizationId, OrganizationCapability>(0L, "IsOrganizationAnchoredOnLocalServer orgId='{0}', capability='{1}' -- No org mailboxes found", orgId, organizationCapability);
                return(false);
            }
            ADUser aduser      = organizationMailboxesByCapability[0];
            Server localServer = LocalServerCache.LocalServer;

            if (localServer == null)
            {
                ExTraceGlobals.StorageTracer.TraceDebug(0L, "IsOrganizationAnchoredOnLocalServer - No local server found");
                return(false);
            }
            List <Guid> list = new List <Guid>(new Guid[]
            {
                aduser.Database.ObjectGuid
            });

            ExTraceGlobals.StorageTracer.TraceDebug <OrganizationId, string, int>(0L, "IsOrganizationAnchoredOnLocalServer orgId='{0}' -- invoking MailboxAdminHelper.GetOnlineDatabase() for server {1} and list of {2} guids", orgId, localServer.Fqdn, list.Count);
            List <Guid> onlineDatabase = MailboxAdminHelper.GetOnlineDatabase(localServer.Fqdn, list);

            ExTraceGlobals.StorageTracer.TraceDebug <OrganizationId, int>(0L, "IsOrganizationAnchoredOnLocalServer orgId='{0}' -- MailboxAdminHelper.GetOnlineDatabase() returned list of {1} guids", orgId, onlineDatabase.Count);
            return(onlineDatabase.Count > 0);
        }