Get count of unread emails from all folders using EWS

Hello,

I am new to EWS and I am trying to find an example to count the number of unread emails from inbox and other folders in Outlook 2013 using EWS.

Can anybody suggest me a complete example of the scenario?

This is the code that I have so far,

           public void getEmailCount(Action<int> callback)

        {
            int unreadCount = 0;

            FolderView viewFolders = new FolderView(int.MaxValue) { Traversal = FolderTraversal.Deep, PropertySet = new PropertySet(BasePropertySet.IdOnly) };
            ItemView viewEmails = new ItemView(int.MaxValue) { PropertySet = new PropertySet(BasePropertySet.IdOnly) };
            SearchFilter unreadFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
            SearchFilter folderFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "AllItems"));

            FindFoldersResults inboxFolders = service.FindFolders(WellKnownFolderName.Root, folderFilter, viewFolders);

            if (inboxFolders.Count() == 0)//if we don't have AllItems folder
            {
                //search all items in Inbox and subfolders
                FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, unreadFilter, viewEmails);
                unreadCount += findResults.Count();

                inboxFolders = service.FindFolders(WellKnownFolderName.Inbox, viewFolders);
                foreach (Folder folder in inboxFolders.Folders)
                {
                    findResults = service.FindItems(folder.Id, unreadFilter, viewEmails);
                    unreadCount += findResults.Count();
                }
            }
            else //AllItems is avilable
            {
                foreach (Folder folder in inboxFolders.Folders)
                {
                    FindItemsResults<Item> findResults = service.FindItems(folder.Id, unreadFilter, viewEmails);
                    unreadCount += findResults.Count();
                }
            }

            callback(unreadCount);
        }

Thanks.

August 5th, 2015 7:12pm

There is a UnreadCount property on each folder you can use to do that so you just need to enumerate all the folders in a Mailbox and take account for paging eg

            Int64 urCount = 0;
            FindFoldersResults ffResults= null;
            FolderView fvFolderView = new FolderView(1000);
            do
            {
                ffResults = service.FindFolders(WellKnownFolderName.MsgFolderRoot, fvFolderView);
                foreach (Folder fld in ffResults)
                {
                    Object unreadCountval = null;
                    if (fld.TryGetProperty(FolderSchema.UnreadCount, out unreadCountval))
                    {
                        urCount += (Int32)unreadCountval;
                    }
                    
                }
                fvFolderView.Offset += ffResults.Folders.Count;
            } while (ffResults.MoreAvailable);
Cheers
Glen


Free Windows Admin Tool Kit Click here and download it now
August 7th, 2015 2:36am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics