private const int FILE_BUFFER_LENGTH = 5000000; // ~5MB

        /// <summary>
        /// Gets site collections in raw form.
        /// </summary>
        /// <param name="packageId">Package to which desired site collections belong.</param>
        /// <param name="organizationId">Organization to which desired site collections belong.</param>
        /// <param name="filterColumn">Filter column name.</param>
        /// <param name="filterValue">Filter value.</param>
        /// <param name="sortColumn">Sort column name.</param>
        /// <param name="startRow">Row index to start from.</param>
        /// <param name="maximumRows">Maximum number of rows to retrieve.</param>
        /// <returns>Site collections that match.</returns>
        public static SharePointEnterpriseSiteCollectionListPaged GetSiteCollectionsPaged(int packageId, int organizationId, string filterColumn, string filterValue, string sortColumn, int startRow, int maximumRows)
        {
            if (IsDemoMode)
            {
                SharePointEnterpriseSiteCollectionListPaged demoResult = new SharePointEnterpriseSiteCollectionListPaged();
                demoResult.SiteCollections = GetSiteCollections(1, false);
                demoResult.TotalRowCount   = demoResult.SiteCollections.Count;
                return(demoResult);
            }

            SharePointEnterpriseSiteCollectionListPaged paged = new SharePointEnterpriseSiteCollectionListPaged();
            DataSet result = PackageController.GetRawPackageItemsPaged(packageId, ResourceGroups.SharepointEnterpriseServer, typeof(SharePointEnterpriseSiteCollection),
                                                                       true, filterColumn, filterValue, sortColumn, startRow, Int32.MaxValue);
            List <SharePointEnterpriseSiteCollection> items = PackageController.CreateServiceItemsList(result, 1).ConvertAll <SharePointEnterpriseSiteCollection>(delegate(ServiceProviderItem item) { return((SharePointEnterpriseSiteCollection)item); });

            if (organizationId > 0)
            {
                items = items.FindAll(delegate(SharePointEnterpriseSiteCollection siteCollection) { return(siteCollection.OrganizationId == organizationId); });
            }
            paged.TotalRowCount = items.Count;

            if (items.Count > maximumRows)
            {
                items.RemoveRange(maximumRows, items.Count - maximumRows);
            }

            paged.SiteCollections = items;

            return(paged);
        }
        private static List <SharePointEnterpriseSiteCollection> GetOrganizationSharePointEnterpriseSiteCollections(int orgId)
        {
            Organization org = OrganizationController.GetOrganization(orgId);

            SharePointEnterpriseSiteCollectionListPaged siteCollections = GetSiteCollectionsPaged(org.PackageId, org.Id, String.Empty, String.Empty, String.Empty, 0, Int32.MaxValue);

            return(siteCollections.SiteCollections);
        }
        /// <summary>
        /// Deletes SharePoint site collections which belong to organization.
        /// </summary>
        /// <param name="organizationId">Site collection id within metabase.</param>
        public static void DeleteSiteCollections(int organizationId)
        {
            Organization org = OrganizationController.GetOrganization(organizationId);
            SharePointEnterpriseSiteCollectionListPaged existentSiteCollections = GetSiteCollectionsPaged(org.PackageId, org.Id, String.Empty, String.Empty, String.Empty, 0, Int32.MaxValue);

            foreach (SharePointEnterpriseSiteCollection existentSiteCollection in existentSiteCollections.SiteCollections)
            {
                DeleteSiteCollection(existentSiteCollection.Id);
            }
        }
        public List <SharePointEnterpriseSiteCollection> GetSharePointEnterpriseSiteCollectionPaged(int packageId, int organizationId, string filterColumn, string filterValue, int maximumRows, int startRowIndex, string sortColumn)
        {
            if (!String.IsNullOrEmpty(filterValue))
            {
                filterValue = filterValue + "%";
            }

            result = ES.Services.HostedSharePointServersEnt.Enterprise_GetSiteCollectionsPaged(packageId, organizationId, filterColumn, filterValue, sortColumn, startRowIndex, maximumRows);

            return(result.SiteCollections);
        }
        public SharePointEnterpriseSiteCollection Enterprise_GetSiteCollectionByDomain(int organizationId, string domain)
        {
            DomainInfo domainInfo = ServerController.GetDomain(domain);
            SharePointEnterpriseSiteCollectionListPaged existentSiteCollections = this.Enterprise_GetSiteCollectionsPaged(domainInfo.PackageId, organizationId, "ItemName", String.Format("%{0}", domain), String.Empty, 0, Int32.MaxValue);

            foreach (SharePointEnterpriseSiteCollection existentSiteCollection in existentSiteCollections.SiteCollections)
            {
                Uri existentSiteCollectionUri = new Uri(existentSiteCollection.Name);
                if (existentSiteCollection.Name == String.Format("{0}://{1}", existentSiteCollectionUri.Scheme, domain))
                {
                    return(existentSiteCollection);
                }
            }

            return(null);
        }
        private void SaveItem()
        {
            if (!Page.IsValid)
            {
                return;
            }


            if (this.SiteCollectionId == 0)
            {
                if (this.userSelector.GetAccount() == null)
                {
                    localMessageBox.ShowWarningMessage("HOSTEDSHAREPOINT_NO_USERS");
                    return;
                }


                // new item
                try
                {
                    item = new SharePointEnterpriseSiteCollection();

                    if (!UseSharedSLL(PanelSecurity.PackageId))
                    {
                        SharePointEnterpriseSiteCollectionListPaged existentSiteCollections = ES.Services.HostedSharePointServersEnt.Enterprise_GetSiteCollectionsPaged(PanelSecurity.PackageId, this.OrganizationId, "ItemName", String.Format("%{0}", this.domain.DomainName), String.Empty, 0, Int32.MaxValue);
                        foreach (SharePointEnterpriseSiteCollection existentSiteCollection in existentSiteCollections.SiteCollections)
                        {
                            Uri existentSiteCollectionUri = new Uri(existentSiteCollection.Name);
                            if (existentSiteCollection.Name == String.Format("{0}://{1}", existentSiteCollectionUri.Scheme, this.txtHostName.Text.ToLower() + "." + this.domain.DomainName))
                            {
                                localMessageBox.ShowWarningMessage("HOSTEDSHAREPOINT_DOMAIN_IN_USE");
                                return;
                            }
                        }

                        item.Name = this.txtHostName.Text.ToLower() + "." + this.domain.DomainName;
                    }
                    else
                    {
                        item.Name = string.Empty;
                    }

                    // get form data

                    item.OrganizationId = this.OrganizationId;
                    item.Id             = this.SiteCollectionId;
                    item.PackageId      = PanelSecurity.PackageId;

                    item.LocaleId    = Int32.Parse(this.ddlLocaleID.SelectedValue);
                    item.OwnerLogin  = this.userSelector.GetSAMAccountName();
                    item.OwnerEmail  = this.userSelector.GetPrimaryEmailAddress();
                    item.OwnerName   = this.userSelector.GetDisplayName();
                    item.Title       = txtTitle.Text;
                    item.Description = txtDescription.Text;


                    item.MaxSiteStorage = maxStorage.QuotaValue;
                    item.WarningStorage = warningStorage.QuotaValue;

                    int result = ES.Services.HostedSharePointServersEnt.Enterprise_AddSiteCollection(item);
                    if (result < 0)
                    {
                        localMessageBox.ShowResultMessage(result);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    localMessageBox.ShowErrorMessage("HOSTEDSHAREPOINT_ADD_SITECOLLECTION", ex);
                    return;
                }
            }
            else
            {
                ES.Services.HostedSharePointServersEnt.Enterprise_UpdateQuota(PanelRequest.ItemID, SiteCollectionId, editMaxStorage.QuotaValue, editWarningStorage.QuotaValue);
            }

            // return
            RedirectToSiteCollectionsList();
        }