EWS get count of unread emails from all folders
Asked Answered
U

4

5

I'm trying to get number of unread emails from Exchange for specific user.

I'm able to get number of emails from Inbox like so:

SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
ItemView view = new ItemView(int.MaxValue);
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, sf, view);
int unreadCount = 0;
foreach (EmailMessage i in findResults)
    {
        unreadCount++;
    }
label1.Text = unreadCount.ToString();

This works great.
I'm also able to get all subfolders is Inbox:

FindFoldersResults findResults1 = service.FindFolders(
    WellKnownFolderName.Inbox,
    new FolderView(int.MaxValue) { Traversal = FolderTraversal.Deep });

foreach (Folder folder in findResults1.Folders)
{
    Console.WriteLine(folder.DisplayName);
}

Problem is that I'm not able to combine these two together.
I know that I can do nested foreach loop, but I would like to avoid that.

I found these question: Exchange Web Services (EWS) FindItems within All Folders, but it requires to at least use Outlook 2010 in order to create AllItems folder.

I know that I can create SearchFilterCollection, but how to add rules to it so that it will search for unread emails in Inbox and all subfolders?

EDIT:

This what I have tried to do so far:

private int getEmailCount()
{
    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));

    FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, unreadFilter, viewEmails);
    unreadCount += findResults.Count();

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

    foreach (Folder folder in inboxFolders.Folders)
    {
        findResults = service.FindItems(folder.Id, unreadFilter, viewEmails);
        unreadCount += findResults.Count();
    }

    return unreadCount;
    }

Basically this works, but when I have created multiple subfolders it started to work very slow.
Instead of multiple queries can I do one to get same results?

Univalence answered 2/10, 2012 at 9:4 Comment(0)
U
4

I've searched a bit and created this function:

    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);
    }

Basically it checks if we have AllItems folder avilable.
If YES then we do one, simple query that returns all unread messages.
If NO then we loop all folders inside Inbox. This is slower and depends on how many folders and levels we have.

Any fixes and improvements are welcome :)

Univalence answered 2/10, 2012 at 13:53 Comment(4)
great code, but it does include the deleted items folder when using AllItems, which might not be ideal in all circumstances.Abingdon
@Abingdon - I just checked and in my case AllItems folder doesn't include deleted items. I'm not saying that this is on every server as I'm not an Exchange expert. Maybe there is soem soft of configuration on Exchange what items should AllItems folder include?Univalence
Are you obliged to create SearchFilterCollection instances when you put only one SearchFilter in them? This looks overkill. (Applies to the original question too.)Ricky
@Ricky - I wasn't obligated to create SearchFilterCollection. I didn't even noticed this, so thanks for pointing it out. I have no performance issues with my code, but removing Collection and leaving single filter seems best option for improvement. In near future I'll be able to experiment with Exchange 2013 so I'll definitively will be looking in this code again. Maybe You have better solution for this? If Yes please post is as answer.Univalence
L
4

Not sure it's what you were looking for, but... Getting unread emails counts (inbox / draft etc.):

int x = Folder.Bind(yourService, WellKnownFolderName.Inbox).UnreadCount;
int y = Folder.Bind(yourService, WellKnownFolderName.Drafts).UnreadCount;
return x + y;

In this case, the service is called two times, but the calls are issued sequentially - not good enough.

Though, you can issue both requests at the same time and increase the response time of your app.

See this or any tutorial that explains how to instantiate two TPL tasks, send them to the task scheduler, wait for both (WaitAll()) to complete and, in the end, retrieve their results :)

And, if you want to get the email counts after applying some custom filters (not the trivial 'unread' filter), then make sure that your ItemView object is ItemView(1), not ItemView(int.MaxValue). Then, get the total count:

int n = findItemsResults.TotalCount;

See the docs for TotalCount property.

This way, the service response is quite small - it contains only one item, but it (the response) also carries the total count... That's what you want, right?

Limber answered 18/1, 2014 at 10:43 Comment(0)
M
1

Here is the code for fetching the total unread count of a mailbox.

  1. Find all folders with deep traversal
  2. Limit the set of properties for each folder to id, unreadcount, displayname (forinformational purpose)
  3. Some of the folders like calendars, contacts don't have this property so handle that case

Limitations with this code snippet:

  1. If the mailbox contains more than 1000 folders under Top of information store, then you will not get the complete total unread count
  2. To handle that case do finditems in a loop until there are no more items with same logic

This makes only one findfolders call and work on the results.

public static int GetTotalUnreadCount(ExchangeService ewsConnector)
{
    int pagedView = 1000;
    FolderView fv = new FolderView(pagedView) { Traversal = Microsoft.Exchange.WebServices.Data.FolderTraversal.Deep };
    fv.PropertySet = new PropertySet(BasePropertySet.IdOnly, FolderSchema.UnreadCount, FolderSchema.DisplayName);

    FindFoldersResults findResults = ewsConnector.FindFolders(WellKnownFolderName.MsgFolderRoot, fv);

    int totalUnreadCount = 0;
    foreach (Folder f in findResults.Folders)
    {
        try
        {
            totalUnreadCount += f.UnreadCount;
            Console.WriteLine("Folder [" + f.DisplayName + "] has [" + f.UnreadCount.ToString() + "] unread items.");
        }
        catch(Exception ex)
        {
            Console.WriteLine("Folder [" + f.DisplayName + "] does not have the unread property.");
        }
    }

    Console.WriteLine("Mailbox has [" + totalUnreadCount.ToString() + "] unread items.");

    return totalUnreadCount;
}
Mellissamellitz answered 3/6, 2015 at 16:3 Comment(0)
D
0

Code example for fetching folders and their counts. In this example, we are listing all first-level folders, and adding them to a common class list of folders, with a NewMessageCount object in there. Key is the Folder.Bind(myService, folder.Id).UnreadCount section.

public List<Common.MailFolder> ListFolders()
    {
        try
        {
            List<Common.MailFolder> myFolders = new List<Common.MailFolder>();
            Common.MailFolder myFolder;

            List<ExchangeFolder> myExchangeFolders = new List<ExchangeFolder>();

            FolderView myView = new FolderView(10);
            myView.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties, FolderSchema.DisplayName, FolderSchema.Id);
            myView.Traversal = FolderTraversal.Shallow;

            FindFoldersResults myResults = myService.FindFolders(WellKnownFolderName.MsgFolderRoot, myView);

            foreach (Folder folder in myResults)
            {
                myFolder = new Common.ICE.MailFolder();
                myFolder.NewMessageCount = Folder.Bind(myService, folder.Id).UnreadCount;
                myFolder.FolderId = folder.Id.ToString();
                myFolder.FolderName = folder.DisplayName;
                myFolders.Add(myFolder);
            }

            return myFolders;
        }
        catch (Exception ex)
        {
            Logger.Log.Error("Exchange Helper - List Folders", ex, Utility.GetUserId());
            return null;
        }
    }
Daubigny answered 14/1, 2015 at 19:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.