/// <summary>
        /// Sync folder hierarchy.
        /// </summary>
        /// <param name="exchangeService">Exchange service.</param>
        /// <returns></returns>
        public static async Task SyncFolderHierarchy(ExchangeService exchangeService)
        {
            ChangeCollection <MailFolderChange> folderChange = null;
            string syncState = null;

            do
            {
                folderChange = await exchangeService.SyncFolderHierarchy(null, syncState);

                syncState = folderChange.SyncState;

                foreach (MailFolderChange mailFolderChange in folderChange.Items)
                {
                    Assert.AreEqual(
                        ChangeType.Created,
                        mailFolderChange.ChangeType);
                }
            } while (folderChange.MoreAvailable);

            SearchFilter          inbox  = new SearchFilter.IsEqualTo(MailFolderObjectSchema.DisplayName, "Inbox");
            FindMailFolderResults result =
                await exchangeService.FindFolders(WellKnownFolderName.MsgFolderRoot, inbox, new MailFolderView());

            MailFolder mf = new MailFolder(exchangeService);

            mf.DisplayName = "kakakoko";
            await mf.SaveAsync(result.MailFolders[0]);

            do
            {
                folderChange = await exchangeService.SyncFolderHierarchy(null, syncState);

                syncState = folderChange.SyncState;

                Assert.AreEqual(
                    1,
                    folderChange.TotalCount);

                Assert.AreEqual(
                    ChangeType.Created,
                    folderChange.Items[0].ChangeType);
            } while (folderChange.MoreAvailable);

            await mf.DeleteAsync();

            do
            {
                folderChange = await exchangeService.SyncFolderHierarchy(null, syncState);

                syncState = folderChange.SyncState;

                Assert.AreEqual(
                    1,
                    folderChange.TotalCount);

                Assert.AreEqual(
                    ChangeType.Deleted,
                    folderChange.Items[0].ChangeType);
            } while (folderChange.MoreAvailable);
        }
        /// <summary>
        /// Basic CRUD operations test.
        /// </summary>
        /// <param name="exchangeService"></param>
        public static async Task CreateReadUpdateDeleteMailFolder(ExchangeService exchangeService)
        {
            MailFolder inbox = await exchangeService.GetAsync <MailFolder>(
                new EntityPath(WellKnownFolderName.Inbox.ToString(),
                               typeof(MailFolder)));

            foreach (MailFolder folder in await exchangeService.FindFolders(inbox.Id, new MailFolderView(10, 0)))
            {
                await folder.DeleteAsync();
            }

            MailFolder folder1 = new MailFolder(exchangeService)
            {
                DisplayName = "MyTestFolder1"
            };

            Assert.IsNull(folder1.Id);
            await folder1.SaveAsync(inbox);

            Assert.IsNotNull(folder1.Id);

            MailFolder folder2 = new MailFolder(exchangeService);

            folder2.DisplayName = "MyTestFolder2";

            Assert.IsNull(folder2.Id);
            await folder2.SaveAsync(inbox);

            Assert.IsNotNull(folder2.Id);

            folder2 = await folder2.Move(folder1.Id);

            folder1.DisplayName = "NewDisplayName";
            await folder1.UpdateAsync();

            Assert.AreEqual(
                "NewDisplayName",
                folder1.DisplayName);

            Assert.AreEqual(
                folder1.Id,
                folder2.ParentFolderId);

            await folder2.DeleteAsync();

            Assert.IsNull(folder2.DisplayName);
            Assert.IsNull(folder2.Id);

            await folder1.DeleteAsync();

            Assert.IsNull(folder1.DisplayName);
            Assert.IsNull(folder1.Id);
        }
        /// <summary>
        /// Test mail folder sync.
        /// </summary>
        /// <param name="exchangeService"></param>
        public static async Task SyncMailFolders(ExchangeService exchangeService)
        {
            string folder1Name = "TempSyncFolder1";
            string folder2Name = "TempSyncFolder2";

            MailFolder msgFolderRoot = await exchangeService.GetAsync <MailFolder>(
                new EntityPath(
                    WellKnownFolderName.MsgFolderRoot.ToString(),
                    typeof(MailFolder)));

            FindMailFolderResults findFolders = await exchangeService.FindFolders(
                WellKnownFolderName.MsgFolderRoot,
                new MailFolderView(30, 0));

            foreach (MailFolder mailFolder in findFolders)
            {
                if (mailFolder.DisplayName == folder1Name ||
                    mailFolder.DisplayName == folder2Name)
                {
                    await mailFolder.DeleteAsync();
                }
            }

            string syncState = null;
            int    counter   = 0;
            ChangeCollection <MailFolderChange> sync;

            do
            {
                sync = await exchangeService.SyncFolderHierarchy(null, syncState);

                syncState = sync.SyncState;

                counter++;
            } while (sync.MoreAvailable || counter == 4);

            Assert.IsFalse(sync.MoreAvailable);

            MailFolder folder1 = new MailFolder(exchangeService);

            folder1.DisplayName = folder1Name;
            await folder1.SaveAsync(msgFolderRoot);

            MailFolder folder2 = new MailFolder(exchangeService);

            folder2.DisplayName = folder2Name;
            await folder2.SaveAsync(msgFolderRoot);

            sync = await exchangeService.SyncFolderHierarchy(null, syncState);

            syncState = sync.SyncState;

            Assert.AreEqual(
                2,
                sync.TotalCount);

            foreach (MailFolderChange change in sync)
            {
                Assert.IsTrue(change.ChangeType == ChangeType.Created);
            }

            await folder1.DeleteAsync();

            await folder2.DeleteAsync();

            sync = await exchangeService.SyncFolderHierarchy(null, syncState);

            Assert.IsTrue(sync.TotalCount == 2);
            foreach (MailFolderChange change in sync)
            {
                Assert.IsTrue(change.ChangeType == ChangeType.Deleted);
            }
        }
示例#4
0
        /// <summary>
        /// Sync messages
        /// </summary>
        public static async Task SyncMessages(ExchangeService exchangeService)
        {
            string folderName = "TempSyncFolder";

            await FunctionalTestHelpers.DeleteFolderIfExist(
                folderName,
                exchangeService,
                WellKnownFolderName.MsgFolderRoot);

            MailFolder folder = await FunctionalTestHelpers.CreateFolder(
                folderName,
                exchangeService,
                WellKnownFolderName.MsgFolderRoot);

            for (int i = 0; i < 10; i++)
            {
                await FunctionalTestHelpers.CreateMessage(
                    i,
                    folder,
                    exchangeService);
            }

            string             syncState   = null;
            MessagePropertySet propertySet = new MessagePropertySet();

            propertySet.Add(MessageObjectSchema.ToRecipients);
            ChangeCollection <MessageChange> syncCollection;
            int counter          = 0;
            int numberOfMessages = 0;

            do
            {
                syncCollection = await exchangeService.SyncFolderItems(
                    folder.Id,
                    propertySet,
                    syncState);

                syncState         = syncCollection.SyncState;
                numberOfMessages += syncCollection.TotalCount;
                counter++;

                foreach (MessageChange itemChange in syncCollection.Items)
                {
                    Assert.IsTrue(
                        itemChange.ChangeType == ChangeType.Created);
                }
            } while (syncCollection.MoreAvailable || counter == 4);

            Assert.IsFalse(syncCollection.MoreAvailable);
            Assert.AreEqual(10, numberOfMessages);

            FindItemResults <Message> items = await exchangeService.FindItems(folder.Id, new MessageView(4));

            for (int i = 0; i < items.TotalCount; i++)
            {
                Message msg = items.Items[i];
                if (i < 2)
                {
                    msg.IsRead = false;
                    await msg.UpdateAsync();
                }
                else
                {
                    await msg.DeleteAsync();
                }
            }

            syncCollection = await exchangeService.SyncFolderItems(
                folder.Id,
                propertySet,
                syncState);

            Assert.IsFalse(syncCollection.MoreAvailable);
            Assert.IsTrue(
                syncCollection.TotalCount == 4);

            int changes = syncCollection.Items.Count(i => i.ChangeType == ChangeType.Deleted);

            Assert.AreEqual(
                2,
                changes);

            changes = syncCollection.Items.Count(i => i.ChangeType == ChangeType.Updated);
            Assert.IsTrue(changes == 2);

            await folder.DeleteAsync();
        }