public static List <ExchangeOrganizationDomain> GetOrganizationDomains(int itemId)
        {
            // load organization item
            ExchangeOrganization item = PackageController.GetPackageItem(itemId) as ExchangeOrganization;

            if (item == null)
            {
                return(null); // organization item not found
            }
            // get Exchange service
            ExchangeServerHostedEdition exchange = GetExchangeService(item.ServiceId);

            // get organization domains
            List <ExchangeOrganizationDomain> domains = new List <ExchangeOrganizationDomain>();

            domains.AddRange(exchange.GetOrganizationDomains(item.Name));
            return(domains);

            //return new List<ExchangeOrganizationDomain>
            //{
            //    new ExchangeOrganizationDomain { Identity = "org101\\domain1.com", Name = "domain1.com", IsDefault = true, IsTemp = false },
            //    new ExchangeOrganizationDomain { Identity = "org101\\org101.tempdomain.com", Name = "org101.tempdomain.com", IsDefault = false, IsTemp = true },
            //    new ExchangeOrganizationDomain { Identity = "org101\\myseconddomain.com", Name = "myseconddomain.com", IsDefault = false, IsTemp = false }
            //};
        }
        public static ExchangeServerHostedEdition GetExchangeService(int serviceId)
        {
            ExchangeServerHostedEdition ws = new ExchangeServerHostedEdition();

            ServiceProviderProxy.Init(ws, serviceId);
            return(ws);
        }
        public static ResultObject DeleteOrganization(int itemId)
        {
            ResultObject result = new ResultObject();

            result.IsSuccess = true;

            try
            {
                // initialize task manager
                TaskManager.StartTask(TaskManagerSource, "DELETE_ORGANIZATION");
                TaskManager.WriteParameter("itemId", itemId);

                // load organization item
                ExchangeOrganization item = PackageController.GetPackageItem(itemId) as ExchangeOrganization;
                if (item == null)
                {
                    return(Error <ResultObject>(OrganizationNotFoundError));
                }

                #region Check Space and Account
                // Check account
                int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
                if (accountCheck < 0)
                {
                    return(Warning <ResultObject>((-accountCheck).ToString()));
                }

                // Check space
                int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);
                if (packageCheck < 0)
                {
                    return(Warning <ResultObject>((-packageCheck).ToString()));
                }
                #endregion

                // get Exchange service
                ExchangeServerHostedEdition exchange = GetExchangeService(item.ServiceId);

                // delete organization
                exchange.DeleteOrganization(item.Name);

                // delete meta-item
                PackageController.DeletePackageItem(itemId);

                return(result);
            }
            catch (Exception ex)
            {
                // log error
                TaskManager.WriteError(ex);

                // exit with error code
                return(Error <ResultObject>(DeleteOrganizationError, ex.Message));
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        public static ResultObject UpdateOrganizationServicePlan(int itemId, int newServiceId)
        {
            ResultObject result = new ResultObject();

            result.IsSuccess = true;

            try
            {
                // initialize task manager
                TaskManager.StartTask(TaskManagerSource, "UPDATE_SERVICE");
                TaskManager.WriteParameter("itemId", itemId);
                TaskManager.WriteParameter("newServiceId", newServiceId);

                // load organization item
                ExchangeOrganization item = PackageController.GetPackageItem(itemId) as ExchangeOrganization;
                if (item == null)
                {
                    return(Error <ResultObject>(OrganizationNotFoundError));
                }

                #region Check Space and Account
                // Check account
                int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsAdmin | DemandAccount.IsActive);
                if (accountCheck < 0)
                {
                    return(Warning <ResultObject>((-accountCheck).ToString()));
                }

                // Check space
                int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);
                if (packageCheck < 0)
                {
                    return(Warning <ResultObject>((-packageCheck).ToString()));
                }
                #endregion

                // get Exchange service
                ExchangeServerHostedEdition exchange = GetExchangeService(item.ServiceId);

                // load service settings to know ProgramID, OfferID
                StringDictionary serviceSettings = ServerController.GetServiceSettings(newServiceId);
                string           programId       = serviceSettings["programID"];
                string           offerId         = serviceSettings["offerID"];

                // check settings
                if (String.IsNullOrEmpty(programId))
                {
                    result.ErrorCodes.Add(ProgramIdIsNotSetError);
                }
                if (String.IsNullOrEmpty(offerId))
                {
                    result.ErrorCodes.Add(OfferIdIsNotSetError);
                }

                // update service plan
                exchange.UpdateOrganizationServicePlan(item.Name, programId, offerId);

                // move item between services
                int moveResult = PackageController.MovePackageItem(itemId, newServiceId);
                if (moveResult < 0)
                {
                    return(Error <ResultObject>((-moveResult).ToString()));
                }

                return(result);
            }
            catch (Exception ex)
            {
                // log error
                TaskManager.WriteError(ex);

                // exit with error code
                return(Error <ResultObject>(UpdateServicePlanError, ex.Message));
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        public static ResultObject UpdateOrganizationCatchAllAddress(int itemId, string catchAllEmail)
        {
            ResultObject result = new ResultObject();

            result.IsSuccess = true;

            try
            {
                // initialize task manager
                TaskManager.StartTask(TaskManagerSource, "UPDATE_CATCHALL");
                TaskManager.WriteParameter("itemId", itemId);
                TaskManager.WriteParameter("catchAllEmail", catchAllEmail);

                // load organization item
                ExchangeOrganization item = PackageController.GetPackageItem(itemId) as ExchangeOrganization;
                if (item == null)
                {
                    return(Error <ResultObject>(OrganizationNotFoundError));
                }

                #region Check Space and Account
                // Check account
                int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
                if (accountCheck < 0)
                {
                    return(Warning <ResultObject>((-accountCheck).ToString()));
                }

                // Check space
                int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);
                if (packageCheck < 0)
                {
                    return(Warning <ResultObject>((-packageCheck).ToString()));
                }
                #endregion

                // get Exchange service
                ExchangeServerHostedEdition exchange = GetExchangeService(item.ServiceId);

                // update catch-all
                exchange.UpdateOrganizationCatchAllAddress(item.Name, catchAllEmail);

                // save new catch-all in the item
                item.CatchAllAddress = catchAllEmail;
                PackageController.UpdatePackageItem(item);

                return(result);
            }
            catch (Exception ex)
            {
                // log error
                TaskManager.WriteError(ex);

                // exit with error code
                return(Error <ResultObject>(UpdateCatchAllError, ex.Message));
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        public static IntResult CreateOrganization(int packageId, string organizationId,
                                                   string domain, string adminName, string adminEmail, string adminPassword)
        {
            IntResult result = new IntResult();

            result.IsSuccess = true;

            try
            {
                // initialize task manager
                TaskManager.StartTask(TaskManagerSource, "CREATE_ORGANIZATION");
                TaskManager.WriteParameter("packageId", packageId);
                TaskManager.WriteParameter("organizationId", organizationId);
                TaskManager.WriteParameter("domain", domain);
                TaskManager.WriteParameter("adminName", adminName);
                TaskManager.WriteParameter("adminEmail", adminEmail);
                TaskManager.WriteParameter("adminPassword", adminPassword);

                // get Exchange service ID
                int serviceId = PackageController.GetPackageServiceId(packageId, ResourceGroups.ExchangeHostedEdition);
                if (serviceId < 1)
                {
                    return(Error <IntResult>(ExchangeServiceNotEnabledError));
                }

                #region Check Space and Account
                // Check account
                int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
                if (accountCheck < 0)
                {
                    return(Warning <IntResult>((-accountCheck).ToString()));
                }

                // Check space
                int packageCheck = SecurityContext.CheckPackage(packageId, DemandPackage.IsActive);
                if (packageCheck < 0)
                {
                    return(Warning <IntResult>((-packageCheck).ToString()));
                }
                #endregion

                // get Exchange service
                ExchangeServerHostedEdition exchange = GetExchangeService(serviceId);

                // load service settings to know ProgramID, OfferID
                StringDictionary serviceSettings = ServerController.GetServiceSettings(serviceId);
                string           programId       = serviceSettings["programID"];
                string           offerId         = serviceSettings["offerID"];

                // check settings
                if (String.IsNullOrEmpty(programId))
                {
                    result.ErrorCodes.Add(ProgramIdIsNotSetError);
                }
                if (String.IsNullOrEmpty(offerId))
                {
                    result.ErrorCodes.Add(OfferIdIsNotSetError);
                }

                if (result.ErrorCodes.Count > 0)
                {
                    result.IsSuccess = false;
                    return(result);
                }

                #region Create organization
                int itemId = -1;
                ExchangeOrganization org = null;
                try
                {
                    // create organization
                    exchange.CreateOrganization(organizationId, programId, offerId, domain, adminName, adminEmail, adminPassword);

                    // save item into meta-base
                    org           = new ExchangeOrganization();
                    org.Name      = organizationId;
                    org.PackageId = packageId;
                    org.ServiceId = serviceId;
                    itemId        = PackageController.AddPackageItem(org);
                }
                catch (Exception ex)
                {
                    // log error
                    TaskManager.WriteError(ex);
                    return(Error <IntResult>(CreateOrganizationError));
                }
                #endregion

                #region Update organization quotas
                // update max org quotas
                UpdateOrganizationQuotas(org);

                // override quotas
                ResultObject quotasResult = ExchangeHostedEditionController.UpdateOrganizationQuotas(itemId,
                                                                                                     org.MaxMailboxCountQuota,
                                                                                                     org.MaxContactCountQuota,
                                                                                                     org.MaxDistributionListCountQuota);

                if (!quotasResult.IsSuccess)
                {
                    return(Error <IntResult>(quotasResult, CreateOrganizationError));
                }
                #endregion

                #region Add temporary domain
                // load settings
                PackageSettings settings           = GetExchangePackageSettings(org);
                string          tempDomainTemplate = settings[TempDomainSetting];
                if (!String.IsNullOrEmpty(tempDomainTemplate))
                {
                    // add temp domain
                    string tempDomain = String.Format("{0}.{1}", domain, tempDomainTemplate);
                    AddOrganizationDomain(itemId, tempDomain);
                }

                #endregion

                result.Value = itemId;
                return(result);
            }
            catch (Exception ex)
            {
                // log error
                TaskManager.WriteError(ex);

                // exit with error code
                return(Error <IntResult>(GeneralError, ex.Message));
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        public static ResultObject UpdateOrganizationQuotas(int itemId, int mailboxesNumber, int contactsNumber, int distributionListsNumber)
        {
            ResultObject result = new ResultObject();

            result.IsSuccess = true;

            try
            {
                // initialize task manager
                TaskManager.StartTask(TaskManagerSource, "UPDATE_QUOTAS");
                TaskManager.WriteParameter("itemId", itemId);
                TaskManager.WriteParameter("mailboxesNumber", mailboxesNumber);
                TaskManager.WriteParameter("contactsNumber", contactsNumber);
                TaskManager.WriteParameter("distributionListsNumber", distributionListsNumber);

                // load organization item
                ExchangeOrganization item = PackageController.GetPackageItem(itemId) as ExchangeOrganization;
                if (item == null)
                {
                    return(Error <ResultObject>(OrganizationNotFoundError));
                }

                #region Check Space and Account
                // Check account
                int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
                if (accountCheck < 0)
                {
                    return(Warning <ResultObject>((-accountCheck).ToString()));
                }

                // Check space
                int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);
                if (packageCheck < 0)
                {
                    return(Warning <ResultObject>((-packageCheck).ToString()));
                }
                #endregion

                // check quotas
                UpdateOrganizationQuotas(item);

                if (item.MaxMailboxCountQuota > -1 && mailboxesNumber > item.MaxMailboxCountQuota)
                {
                    return(Error <ResultObject>(UpdateQuotasWrongQuotaError));
                }
                if (item.MaxContactCountQuota > -1 && contactsNumber > item.MaxContactCountQuota)
                {
                    return(Error <ResultObject>(UpdateQuotasWrongQuotaError));
                }
                if (item.MaxDistributionListCountQuota > -1 && distributionListsNumber > item.MaxDistributionListCountQuota)
                {
                    return(Error <ResultObject>(UpdateQuotasWrongQuotaError));
                }

                // get Exchange service
                ExchangeServerHostedEdition exchange = GetExchangeService(item.ServiceId);

                // update quotas
                exchange.UpdateOrganizationQuotas(item.Name, mailboxesNumber, contactsNumber, distributionListsNumber);

                return(result);
            }
            catch (Exception ex)
            {
                // log error
                TaskManager.WriteError(ex);

                // exit with error code
                return(Error <ResultObject>(UpdateQuotasError, ex.Message));
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        public static ResultObject AddOrganizationDomain(int itemId, string domainName)
        {
            ResultObject result = new ResultObject();

            result.IsSuccess = true;

            try
            {
                // initialize task manager
                TaskManager.StartTask(TaskManagerSource, "ADD_DOMAIN");
                TaskManager.WriteParameter("itemId", itemId);
                TaskManager.WriteParameter("domain", domainName);

                // load organization item
                ExchangeOrganization item = PackageController.GetPackageItem(itemId) as ExchangeOrganization;
                if (item == null)
                {
                    return(Error <ResultObject>(OrganizationNotFoundError));
                }

                #region Check Space and Account
                // Check account
                int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
                if (accountCheck < 0)
                {
                    return(Warning <ResultObject>((-accountCheck).ToString()));
                }

                // Check space
                int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);
                if (packageCheck < 0)
                {
                    return(Warning <ResultObject>((-packageCheck).ToString()));
                }
                #endregion

                // get organization details
                ExchangeOrganization org = GetOrganizationDetails(item.Id);
                if (org == null)
                {
                    return(Error <ResultObject>(OrganizationNotFoundError));
                }

                // check domains quota
                if (org.MaxDomainsCountQuota > -1 && org.Domains.Length >= org.MaxDomainsCountQuota)
                {
                    return(Error <IntResult>(AddDomainQuotaExceededError));
                }

                // check if the domain already exists
                DomainInfo domain      = null;
                int        checkResult = ServerController.CheckDomain(domainName);
                if (checkResult == BusinessErrorCodes.ERROR_DOMAIN_ALREADY_EXISTS)
                {
                    // domain exists
                    // check if it belongs to the same space
                    domain = ServerController.GetDomain(domainName);
                    if (domain == null)
                    {
                        return(Error <ResultObject>((-checkResult).ToString()));
                    }

                    if (domain.PackageId != org.PackageId)
                    {
                        return(Error <ResultObject>((-checkResult).ToString()));
                    }

                    // check if domain is already used in this organization
                    foreach (ExchangeOrganizationDomain orgDomain in org.Domains)
                    {
                        if (String.Equals(orgDomain.Name, domainName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            return(Error <ResultObject>(AddDomainAlreadyUsedError));
                        }
                    }
                }
                else if (checkResult == BusinessErrorCodes.ERROR_RESTRICTED_DOMAIN)
                {
                    return(Error <ResultObject>((-checkResult).ToString()));
                }

                // create domain if required
                if (domain == null)
                {
                    domain                = new DomainInfo();
                    domain.PackageId      = org.PackageId;
                    domain.DomainName     = domainName;
                    domain.IsInstantAlias = false;
                    domain.IsSubDomain    = false;

                    int domainId = ServerController.AddDomain(domain);
                    if (domainId < 0)
                    {
                        return(Error <ResultObject>((-domainId).ToString()));
                    }

                    // add domain
                    domain.DomainId = domainId;
                }

                // get Exchange service
                ExchangeServerHostedEdition exchange = GetExchangeService(item.ServiceId);

                // add domain
                exchange.AddOrganizationDomain(item.Name, domainName);

                return(result);
            }
            catch (Exception ex)
            {
                // log error
                TaskManager.WriteError(ex);

                // exit with error code
                return(Error <ResultObject>(AddDomainError, ex.Message));
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        public static ExchangeOrganization GetOrganizationDetails(int itemId)
        {
            // load organization item
            ExchangeOrganization item = PackageController.GetPackageItem(itemId) as ExchangeOrganization;

            if (item == null)
            {
                return(null); // organization item not found
            }
            // get Exchange service
            ExchangeServerHostedEdition exchange = GetExchangeService(item.ServiceId);

            // get organization details
            ExchangeOrganization org = exchange.GetOrganizationDetails(item.Name);

            //ExchangeOrganization org = new ExchangeOrganization
            //{
            //    Id = item.Id,
            //    PackageId = item.PackageId,
            //    ServiceId = item.ServiceId,
            //    Name = item.Name,
            //    AdministratorEmail = "*****@*****.**",
            //    AdministratorName = "Administrator Mailbox",
            //    CatchAllAddress = "",
            //    ContactCount = 1,
            //    ContactCountQuota = 2,
            //    MaxContactCountQuota = 3,
            //    DistinguishedName = "DN=....",
            //    DistributionListCount = 2,
            //    DistributionListCountQuota = 3,
            //    MaxDistributionListCountQuota = 3,
            //    MaxDomainsCountQuota = 4,
            //    ExchangeControlPanelUrl = "http://ecp.domain.com",
            //    MailboxCount = 3,
            //    MailboxCountQuota = 4,
            //    MaxMailboxCountQuota = 4,
            //    ProgramId = "HostedExchange",
            //    OfferId = "2",
            //    ServicePlan = "HostedExchange_Basic",
            //    Domains = GetOrganizationDomains(item.Id).ToArray()
            //};

            // update item props
            org.Id              = item.Id;
            org.PackageId       = item.PackageId;
            org.ServiceId       = item.ServiceId;
            org.Name            = item.Name;
            org.CatchAllAddress = item.CatchAllAddress;

            // update max quotas
            UpdateOrganizationQuotas(org);

            // process summary information
            org.SummaryInformation = GetExchangeOrganizationSummary(org);

            // process domains
            PackageSettings settings = GetExchangePackageSettings(org);

            if (settings != null)
            {
                // get settings
                string tempDomain = settings[TempDomainSetting];
                string ecpUrl     = settings[ExchangeControlPanelUrlSetting];

                // iterate through domains
                foreach (ExchangeOrganizationDomain domain in org.Domains)
                {
                    if (tempDomain != null && domain.Name.EndsWith("." + tempDomain, StringComparison.InvariantCultureIgnoreCase))
                    {
                        domain.IsTemp = true;
                    }
                    if (domain.IsDefault && ecpUrl != null)
                    {
                        org.ExchangeControlPanelUrl = Utils.ReplaceStringVariable(ecpUrl, "domain_name", domain.Name);
                    }
                }
            }

            // return org
            return(org);
        }