private List <VersionedId> GetIdsFromFolderNames(MailboxSession mailboxSession, string[] folderNames)
        {
            OptInFolders.< > c__DisplayClass11 CS$ < > 8__locals1 = new OptInFolders.< > c__DisplayClass11();
            CS$ < > 8__locals1.folderNames = folderNames;
            List <VersionedId>       list              = new List <VersionedId>();
            List <MailboxFolderData> list2             = null;
            MailboxFolderData        mailboxFolderData = null;

            ProvisionedFolderReader.GetProvisionedFoldersFromMailbox(mailboxSession, true, out mailboxFolderData, out list2);
            if (list2 == null || list2.Count == 0)
            {
                throw new ELCPartialCompletionException(Strings.descFailedToCreateOneOrMoreOrganizationalFolders(Strings.descFailedToGetOrganizationalFoldersForMailbox(this.mailboxToAccess.MailboxInfo.PrimarySmtpAddress.ToString())));
            }
            OptInFolders.Tracer.TraceDebug <int, string>((long)this.GetHashCode(), "Retrieved '{0}' Managed Custom Folder(s) for mailbox '{1}'.", list2.Count, this.mailboxToAccess.MailboxInfo.PrimarySmtpAddress.ToString());
            int folderIndex;

            for (folderIndex = 0; folderIndex < CS$ < > 8__locals1.folderNames.GetLength(0); folderIndex++)
            {
                int num = list2.FindIndex((MailboxFolderData matchedFolder) => string.Compare(CS$ < > 8__locals1.folderNames[folderIndex], matchedFolder.Name, StringComparison.OrdinalIgnoreCase) == 0);
                if (num == -1)
                {
                    throw new ELCPartialCompletionException(Strings.descFailedToCreateOneOrMoreOrganizationalFolders(Strings.descCannotFindOrganizationalFolderInMailbox(CS$ < > 8__locals1.folderNames[folderIndex], this.mailboxToAccess.MailboxInfo.PrimarySmtpAddress.ToString())));
                }
                list.Add(list2[num].Id);
            }
            return(list);
        }
Exemple #2
0
        internal static StoreObjectId GetElcRootFolderId(MailboxSession mailboxSession)
        {
            StoreObjectId result = null;
            string        text   = null;
            string        text2  = null;

            ProvisionedFolderReader.GetElcRootFolderInfo(mailboxSession, out result, out text, out text2);
            return(result);
        }
        internal VersionedId[] CreateOrganizationalFolders(string[] folderNames)
        {
            OptInFolders.< > c__DisplayClass2 CS$ < > 8__locals1 = new OptInFolders.< > c__DisplayClass2();
            CS$ < > 8__locals1.folderNames = folderNames;
            if (CS$ < > 8__locals1.folderNames == null)
            {
                throw new ArgumentNullException();
            }
            if (CS$ < > 8__locals1.folderNames.Length == 0)
            {
                throw new ArgumentException("The number of input folders cannot be zero.");
            }
            if (CS$ < > 8__locals1.folderNames.Length > 1000)
            {
                throw new ArgumentException("The number of input folders exceeded allowable limit.");
            }
            List <VersionedId> list = new List <VersionedId>();
            int folderIndex;

            for (folderIndex = 0; folderIndex < CS$ < > 8__locals1.folderNames.GetLength(0); folderIndex++)
            {
                int length = Array.FindAll <string>(CS$ < > 8__locals1.folderNames, (string matchedFolderName) => string.Compare(CS$ < > 8__locals1.folderNames[folderIndex], matchedFolderName, StringComparison.OrdinalIgnoreCase) == 0).GetLength(0);
                if (length > 1)
                {
                    OptInFolders.Tracer.TraceDebug <string>((long)this.GetHashCode(), "Input folder list contains duplicate entry of '{0}'.", CS$ < > 8__locals1.folderNames[folderIndex]);
                    throw new ELCDuplicateFolderNamesArgumentException(CS$ < > 8__locals1.folderNames[folderIndex]);
                }
            }
            this.ConfirmCanActAsOwner();
            OptInFolders.Tracer.TraceDebug <string>((long)this.GetHashCode(), "WindowsPrincipal validation for mailbox '{0}' successful.", this.mailboxToAccess.MailboxInfo.PrimarySmtpAddress.ToString());
            using (MailboxSession mailboxSession = MailboxSession.OpenAsAdmin(this.mailboxToAccess, CultureInfo.InvariantCulture, "Client=ELC;Action=Create Managed Folders"))
            {
                if (this.budget != null)
                {
                    mailboxSession.AccountingObject = this.budget;
                }
                Folder folder = null;
                try
                {
                    List <AdFolderData> adFoldersFromFolderNames = this.GetAdFoldersFromFolderNames(CS$ < > 8__locals1.folderNames);
                    MailboxFolderData   mailboxFolderData        = null;
                    OptInFolders.Tracer.TraceDebug <int, string>((long)this.GetHashCode(), "Need to create '{0}' Managed Custom Folders for mailbox '{1}'.", adFoldersFromFolderNames.Count, this.mailboxToAccess.MailboxInfo.PrimarySmtpAddress.ToString());
                    List <MailboxFolderData> list2;
                    ProvisionedFolderReader.GetProvisionedFoldersFromMailbox(mailboxSession, true, out mailboxFolderData, out list2);
                    if (list2 != null && list2.Count != 0)
                    {
                        using (List <AdFolderData> .Enumerator enumerator = adFoldersFromFolderNames.GetEnumerator())
                        {
                            while (enumerator.MoveNext())
                            {
                                AdFolderData folderToCreate = enumerator.Current;
                                int          num            = list2.FindIndex((MailboxFolderData mbxFolder) => mbxFolder.ElcFolderGuid == folderToCreate.Folder.Guid);
                                if (num >= 0)
                                {
                                    OptInFolders.Tracer.TraceDebug <string, SmtpAddress>((long)this.GetHashCode(), "Folder '{0}' exists in mailbox '{1}'.", folderToCreate.Folder.FolderName, this.mailboxToAccess.MailboxInfo.PrimarySmtpAddress);
                                    throw new ELCOrgFolderExistsException(folderToCreate.Folder.FolderName);
                                }
                            }
                        }
                    }
                    VersionedId versionedId = (mailboxFolderData == null) ? null : mailboxFolderData.Id;
                    if (versionedId == null)
                    {
                        string elcRootUrl = AdFolderReader.GetElcRootUrl(this.budget);
                        folder = ProvisionedFolderCreator.CreateELCRootFolder(mailboxSession, elcRootUrl);
                        if (folder == null)
                        {
                            OptInFolders.Tracer.TraceError <SmtpAddress>((long)this.GetHashCode(), "{0}: Unable to create the ELC root folder in the mailbox.", this.mailboxToAccess.MailboxInfo.PrimarySmtpAddress);
                            throw new ELCRootFailureException(Strings.descFailedToCreateELCRoot(mailboxSession.MailboxOwner.MailboxInfo.PrimarySmtpAddress.ToString()), null);
                        }
                        versionedId = folder.Id;
                    }
                    if (versionedId == null)
                    {
                        OptInFolders.Tracer.TraceDebug <SmtpAddress>((long)this.GetHashCode(), "Unable to create the Managed Folders root folder in mailbox '{0}'.", this.mailboxToAccess.MailboxInfo.PrimarySmtpAddress);
                        throw new ELCRootFailureException(Strings.descFailedToCreateELCRoot(this.mailboxToAccess.MailboxInfo.PrimarySmtpAddress.ToString()), null);
                    }
                    try
                    {
                        if (folder == null)
                        {
                            folder = Folder.Bind(mailboxSession, versionedId);
                        }
                    }
                    catch (ObjectNotFoundException innerException)
                    {
                        OptInFolders.Tracer.TraceDebug <string>((long)this.GetHashCode(), "Failed to bind to the Managed Folders root folder in mailbox '{0}'.", this.mailboxToAccess.MailboxInfo.PrimarySmtpAddress.ToString());
                        throw new ELCRootFailureException(Strings.descCannotBindToElcRootFolder(versionedId.ToString(), this.mailboxToAccess.MailboxInfo.PrimarySmtpAddress.ToString()), innerException);
                    }
                    foreach (AdFolderData adFolderInfo in adFoldersFromFolderNames)
                    {
                        ProvisionedFolderCreator.CreateOneELCFolderInMailbox(adFolderInfo, mailboxSession, folder);
                    }
                    list = this.GetIdsFromFolderNames(mailboxSession, CS$ < > 8__locals1.folderNames);
                }
                finally
                {
                    if (folder != null)
                    {
                        folder.Dispose();
                        folder = null;
                    }
                }
            }
            return(list.ToArray());
        }
        internal static void GetProvisionedFoldersFromMailbox(MailboxSession mbxSession, bool orgOnly, out MailboxFolderData elcRootFolderData, out List <MailboxFolderData> provisionedFolders)
        {
            provisionedFolders = null;
            elcRootFolderData  = null;
            string text = mbxSession.MailboxOwner.MailboxInfo.PrimarySmtpAddress.ToString();

            try
            {
                using (Folder folder = Folder.Bind(mbxSession, DefaultFolderType.Root, ProvisionedFolderReader.ElcFolderProps))
                {
                    object[]          properties = folder.GetProperties(ProvisionedFolderReader.ElcFolderProps);
                    MailboxFolderData folderData = ProvisionedFolderReader.GetFolderData(properties, text);
                    if (folderData != null)
                    {
                        ProvisionedFolderReader.Tracer.TraceDebug <string>(0L, "Mailbox '{0}' has the All-Others policy", text);
                        provisionedFolders = new List <MailboxFolderData>();
                        provisionedFolders.Add(folderData);
                    }
                    using (QueryResult queryResult = folder.FolderQuery(FolderQueryFlags.DeepTraversal, null, null, ProvisionedFolderReader.ElcFolderProps))
                    {
                        queryResult.SeekToOffset(SeekReference.OriginBeginning, 0);
                        if (queryResult.EstimatedRowCount < 1)
                        {
                            ProvisionedFolderReader.Tracer.TraceDebug <string>(0L, "'{0}': No folders exist in the mailbox.", text);
                        }
                        else
                        {
                            for (;;)
                            {
                                object[][] rows = queryResult.GetRows(100);
                                if (rows.Length <= 0)
                                {
                                    break;
                                }
                                for (int i = 0; i < rows.GetLength(0); i++)
                                {
                                    object[]          propValues  = rows[i];
                                    MailboxFolderData folderData2 = ProvisionedFolderReader.GetFolderData(propValues, text);
                                    if (folderData2 != null)
                                    {
                                        if ((folderData2.Flags & (ELCFolderFlags.Provisioned | ELCFolderFlags.Protected | ELCFolderFlags.ELCRoot)) == (ELCFolderFlags.Provisioned | ELCFolderFlags.Protected | ELCFolderFlags.ELCRoot))
                                        {
                                            elcRootFolderData = folderData2;
                                        }
                                        else if (orgOnly && !folderData2.IsOrganizationalFolder())
                                        {
                                            ProvisionedFolderReader.Tracer.TraceDebug <string, string>(0L, "'{0}': Skipping default provisioned folder '{1}' in mailbox because the caller has requested org-only.", text, folderData2.Name);
                                        }
                                        else
                                        {
                                            if (provisionedFolders == null)
                                            {
                                                provisionedFolders = new List <MailboxFolderData>();
                                            }
                                            provisionedFolders.Add(folderData2);
                                            ProvisionedFolderReader.Tracer.TraceDebug <string, string>(0L, "'{0}': has provisioned folder '{1}'", text, folderData2.Name);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (ObjectNotFoundException arg)
            {
                ProvisionedFolderReader.Tracer.TraceError <string, ObjectNotFoundException>(0L, "'{0}': Unable to get the list of provisioned folders from the mailbox - error '{1}'.", text, arg);
                elcRootFolderData  = null;
                provisionedFolders = null;
            }
        }