Monday, January 14, 2008

Finding the number of Unread Voicemail Messages in a Mailbox

One thing that you can be sure to expect to see more in the future is richer types of messages arriving in your inbox one example of this is Voice Mail Messages in Exchange 2007. The interesting thing about this is the way this changes the dynamics of a simple thing like the unread message count in your mailbox. For example how do you now balance the priority of a voice message to that of email message eg which one do you read first (or listen to first). How do you weigh the contents of a voice message to that of a text message and if your away for a longer period of time and amass a larger number of voice and email message in your mailbox can you really get away with just marking them all as read.

Okay maybe I can’t reengineer the space time continuum with this post to solve this but I can show a few ways of show how many unread voicemails you have in a mailbox.

The usually way of getting the unread message count on your inbox is just to use the unread property of the mailbox folder PR_CONTENT_UREAD . Because of the rich data types in the mailbox this property isn’t going to give you the detail of how many unread voice mail messages are in a folder. To find this you have to Search or Filter the folder in question depending on the API your using. For Example in CDO 1.2 you can create a filter on the Inbox to filter all the messages that are unread and have a message class of Type IPM.Note.Microsoft.Voicemail.UM.CA eg

Set msMapiSession = CreateObject("MAPI.Session")
on error Resume next
msMapiSession.Logon "","",False,True,True,True,Servername & vbLF & MailboxAlias
if err.number = 0 then
on error goto 0
set ifInboxFolderCol = msMapiSession.inbox.messages
set vmFilter = ifInboxFolderCol.Filter
vmFilter.Unread = True
Set vmFilterFiled = mFilter.Fields.Add(&h001A001E,"IPM.Note.Microsoft.Voicemail.UM.CA") Wscript.echo mailboxAlias & " " & ifInboxFolderCol.Count

Wscript.echo "Error Opening Mailbox"
end if
Set msMapiSession = Nothing
Set mrMailboxRules = Nothing

With Exchange Web Services you need to use a Finditems request and apply a restriction to that request. A restriction that will limit both unread and Voice mail item would look like.

RestrictionType ffRestriction = new RestrictionType();
AndType raRestictionAnd = new AndType();
raRestictionAnd.Items = new SearchExpressionType[2];
ContainsExpressionType ceContainsVM = new ContainsExpressionType();
ceContainsVM.ContainmentComparison = ContainmentComparisonType.IgnoreCase;

ceContainsVM.ContainmentComparisonSpecified = true;
ceContainsVM.ContainmentMode = ContainmentModeType.FullString;
ceContainsVM.ContainmentModeSpecified = true;
PathToUnindexedFieldType icItemClassProperty = new PathToUnindexedFieldType();
icItemClassProperty.FieldURI = UnindexedFieldURIType.itemItemClass;
ceContainsVM.Item = icItemClassProperty;
ConstantValueType cvConstant = new ConstantValueType();
cvConstant.Value = "IPM.Note.Microsoft.Voicemail.UM.CA";
ceContainsVM.Constant = cvConstant;
IsEqualToType ieToTypeRead = new IsEqualToType();
PathToUnindexedFieldType rsReadStatus = new PathToUnindexedFieldType();
rsReadStatus.FieldURI = UnindexedFieldURIType.messageIsRead;
ieToTypeRead.Item = rsReadStatus;
FieldURIOrConstantType isReadFalse = new FieldURIOrConstantType();
isReadFalse.Item = new ConstantValueType();
(isReadFalse.Item as ConstantValueType).Value = "";
ieToTypeRead.FieldURIOrConstant = isReadFalse;
raRestictionAnd.Items[0] = ceContainsVM;
raRestictionAnd.Items[1] = ieToTypeRead;
ffRestriction.Item = raRestictionAnd;

These two examples can be used to get the number of unread voice mail for individual accounts if you wanted to do this for every UMenabled mailbox on a server you would need to use one of a few methods to find mailboxes that are UMenabled. The easiest is to use the Get-UMMailbox Exchange Management Shell cmdlet . In VBS you can use ADSI and query for mailboxes that msExchUMEnabledFlags AD property set. I’ve put together a VBS sample that will query for all mailbox on a server that are UMenabled using ADSI and then access each mailbox and find the number of unread voice mail and compile the result to text file. I’ve included this script in the download along with the C# EWS finditem sample you can download this from here.

No comments: