Wednesday, February 13, 2013

Mailbox Info Mail app for Exchange 2013/Outlook 2013

Mail Apps for Outlook (and OWA) is one of the really cool new features of Exchange 2013.Anybody who has every had to do any OWA customization using forms from Exchange 2003 to 2010 would be glad to see they have finally gone past their used by date and Mail Apps provides the promise of a unified custom app framework across mail clients. At RTM the feature set gap compared to currently what you can do in Outlook Plugins and full access to all EWS operations is substantial (and frustrating) and a show stopper for a lot of good application ideas, but hopefully this will narrow somewhat like a lot of Exchange features when we see new bits and pieces come through in the new cumulative updates. 

Mail Apps have three main ingredients
  • A manifest file which is what is used to install a Mail App and contains all the configuration and activation rules for the Mail App
  • A html file that is rendered by the Mail client and controls the display elements.
  • JavaScript files that are bascially the heart soul of your application and contain all the Application and Business logic. 
If your going to write a Mail App you need to have a good grip on JavaScript and get acquainted with the JavaScript Api for Office. From a technical point of view you should also understand how the Runtime works which there is a good explanation of here .

Browser Hell - Writing a mail app that you want to run in any browser will take a lot of testing and tweaking, although some of the vendors have improved browser standards are still pretty lacking. For example to get this small Info Apps to work across Chrome, FireFox and ie9 required dealing with Google's different implementation of parseFromString which at the moment can't deal with prefixes while IE and firefox can. This is just one example, if you going to write mail apps make sure you put on your boots and get set to wade in.

Hosting is one other requirement for Mail Apps as the HTML,Manifest and JavaScript files that go along with your application need to be hosted somewhere (they are not stored in Exchange). Basically you just need a Web Server the most obvious source would be your CAS server but any IIS instance will do. If your Mail Apps are mission critical then availability has to factor into the argument or if your in the cloud you'll need to find a Cloud source for your apps. For this sample I've used Azure http://www.windowsazure.com/en-us/home/scenarios/web-sites/ which is a cheap and easy especially if you have a MSDN subscription. 

For this Post I've put together an Info App that uses various methods in the Office JavaScript API and EWS to get the following information.

DisplayName from the Item properties
EmailAddress from the Item properties
TimeZone from the Item properties.
Language from the Mailbox Context.
HostVersion from the Diag Object
UserAgent from the Hosting Browsers window.navigator
Total Mailbox Folder Count from EWS (FindFolder)
MailboxServer from the EWS PR_REPLICA_SERVER Extended property 
Inbox Total Item Count EWS (GetFolder)
Inbox Unread Item Count EWS (GetFolder)

This is what it looks like the App Box



Manifest file

Every Mail App starts with a Manifest file which contains the configuration information about the MailApp. All Mail Apps need a unique GUID which you can generate easily in Powershell using

[System.GUID]::NewGuid()

Permissions - Mail Apps have a three tired permission model if you want to use EWS you need to specify ReadWriteMailbox. A Mail Aps that is going to use EWS needs to be installed and enabled by an administrator. Eg for this apps the following is configured in the Manifest file

  1. <permissions>ReadWriteMailbox</permissions>
Activation Rules -  All Mail apps need an activation rule which basically controls in what items they will appear. Activation rules are also affected by the Permissions that a Mail app requests but for this simple app I have an activation rule that says my Mail App will appear on every Mail Item

  1. <Rule xsi:type="ItemIs" ItemType="Message" /> 
DisplaySetting - With the Display Setting in a manifest file you can control the size of the Application box and how that application box will appear on different form factors like tablets and mobiles.

HTML and JavaScript files, for your Mail apps you need to reference the Office JavaScript file either that you host along with the application or you can use Microsoft's CDN for this sample I'm using the CDN eg.

  1. <script src="https://appsforoffice.microsoft.com/lib/1.0/hosted/office.js" type="text/javascript"></script>  
In this App I've also made use of JQuery which is great utility script to help Glue your Mail App together, like the JavaScript API I've used the CDN version of JQuery.

Installation - There are a couple of way to install a Mail App by far the easiest is just to use EAC eg for this sample Apps if you want to use my hosted version the URL would be

https://gsmailapp.azurewebsites.net/InfoApp/manifest.xml

You should however download and host it yourself I've put a download of the files here

Monday, February 04, 2013

Creating a Report of the WhiteSpace in all the PST's and OST in the default Outlook profile

One of my favorite development utilities is mfcMapi which along with OutlookSpy are invaluable utilities to use when doing any Exchange development. Along with mfcMapi Stephan also has a utility called mrMapi which provides console access to a number of useful MAPI functions . Last week he added a new feature to report on the files sizes and white-space inside a PST or OST based on the Header information in the file http://blogs.msdn.com/b/stephen_griffin/archive/2013/01/28/january-2013-release-of-mfcmapi-and-mrmapi.aspx.

I thought this was a really useful thing as it can be a little bit confounding sometimes looking at PST files that have been produced by exporting a Mailbox and comparing that against the actual mailbox size. So I decided to put a script together that would get all the PST and OST file paths for default Outlook profile. Run those filepaths through mrMapi and parse console Output and the put together a HTML report of the information that ended up looking something like this (all sizes are in MB)


To use this script you need the latest copy of mrMapi from http://mfcmapi.codeplex.com/ and you need to adjust the following varible in the script to the path where you downloaded it to

$mrMapiPath = "c:\mfcmapi\mrmapi.exe"

I've put a download of the script here the code itself looks like

  1. $mrMapiPath = "c:\mfcmapi\mrmapi.exe"  
  2. $rptCollection = @()  
  3.   
  4. function ParseBitValue($String){  
  5.     $numItempattern = '(?=\().*(?=bytes)'  
  6.     $matchedItemsNumber = [regex]::matches($String, $numItempattern)   
  7.     $bytes = [INT64]$matchedItemsNumber[0].Value.Replace("(","").Replace(",","") /1MB  
  8.     return [System.Math]::Round($bytes,2)  
  9. }  
  10.   
  11. $Encode = new-object System.Text.UnicodeEncoding  
  12. ##Check for Office 2013  
  13. $RootKey = "Software\Microsoft\Office\15.0\Outlook\Profiles\" 
  14. $pkProfileskey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($RootKey, $true) 
  15.  
  16. if($pkProfileskey -eq $null){ 
  17.     $RootKey = "Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles" 
  18.     $pkProfileskey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($RootKey, $true) 
  19.     $defProf = $pkProfileskey.GetValue("DefaultProfile") 
  20. } 
  21. else{ 
  22.     $OutDefault = "Software\Microsoft\Office\15.0\Outlook\" 
  23.     $pkProfileskey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($OutDefault, $true) 
  24.     $defProf = $pkProfileskey.GetValue("DefaultProfile") 
  25. } 
  26. $defProf 
  27. $pkSubProfilekey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey(($RootKey + "\\" + $defProf), $true) 
  28. foreach($Valuekey in $pkSubProfilekey.getSubKeyNames()){ 
  29.     $pkSubValueKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey(($RootKey + "\\" + $defProf + "\\" + $Valuekey ), $true) 
  30.     $pstVal = $pkSubValueKey.GetValue("001f6700") 
  31.     if($pstVal -ne $null) 
  32.     { 
  33.         $pstPath = $Encode.GetString($pstVal)  
  34.         $fnFileName = $pstPath.Replace([System.Convert]::ToChar(0x0).ToString().Trim(), "")      
  35.         if(Test-Path $fnFileName){ 
  36.             $rptObj = "" | Select Name,Type,FilePath,FileSize,FreeSpace,PrecentFree 
  37.             $fiInfo = ([System.IO.FileInfo]$fnFileName)  
  38.             $rptObj.Name = $fiInfo.Name 
  39.             $rptObj.FilePath = $fiInfo.DirectoryName 
  40.             $rptObj.Type = "PST"             
  41.             iex ($mrMapiPath + " -pst -i '$pstPath'") | foreach-object{ 
  42.                 if($_.Contains("=")){ 
  43.                     $splitArray = $_.Split("=")              
  44.                     switch ($splitArray[0].Trim()){                  
  45.                         "File Size" { 
  46.                                 $rptObj.FileSize =  ParseBitValue($splitArray[1].Trim())  
  47.                             } 
  48.                         "Free Space" { 
  49.                                 $rptObj.FreeSpace = ParseBitValue($splitArray[1].Trim())  
  50.                             } 
  51.                         "Percent free" { 
  52.                                 $rptObj.PrecentFree = $splitArray[1].Trim() 
  53.                             } 
  54.                     } 
  55.                      
  56.                 } 
  57.             } 
  58.             $rptCollection += $rptObj 
  59.         } 
  60.     } 
  61.     ## Check OSTs 
  62.     $ostVal = $pkSubValueKey.GetValue("001f6610") 
  63.     if($ostVal -ne $null) 
  64.     { 
  65.         $ostPath = $Encode.GetString($ostVal)  
  66.         $fnFileName = $ostPath.Replace([System.Convert]::ToChar(0x0).ToString().Trim(), "")      
  67.         if(Test-Path $fnFileName){ 
  68.             $rptObj = "" | Select Name,Type,FilePath,FileSize,FreeSpace,PrecentFree 
  69.             $fiInfo = ([System.IO.FileInfo]$fnFileName)  
  70.             $rptObj.Name = $fiInfo.Name 
  71.             $rptObj.FilePath = $fiInfo.DirectoryName 
  72.             $rptObj.Type = "OST" 
  73.             iex ($mrMapiPath + " -pst -i `"$fnFileName`"") | foreach-object{ 
  74.                 if($_.Contains("=")){ 
  75.                     $splitArray = $_.Split("=")              
  76.                      
  77.                     switch ($splitArray[0].Trim()){                  
  78.                         "File Size" { 
  79.                                 $rptObj.FileSize =  ParseBitValue($splitArray[1].Trim())  
  80.                             } 
  81.                         "Free Space" { 
  82.                                 $rptObj.FreeSpace = ParseBitValue($splitArray[1].Trim())  
  83.                             } 
  84.                         "Percent free" { 
  85.                                 $rptObj.PrecentFree = $splitArray[1].Trim() 
  86.                             } 
  87.                     } 
  88.                      
  89.                 } 
  90.             } 
  91.             $rptCollection += $rptObj 
  92.         } 
  93.     } 
  94. } 
  95.  
  96.  
  97. $tableStyle = @"  
  98. <style>  
  99. BODY{background-color:white;}  
  100. TABLE{border-width: 1px;  
  101. border-style: solid;  
  102. border-color: black;  
  103. border-collapse: collapse;  
  104. }  
  105. TH{border-width: 1px;  
  106. padding: 10px;  
  107. border-style: solid;  
  108. border-color: black;  
  109. background-color:thistle  
  110. }  
  111. TD{border-width: 1px;  
  112. padding: 2px;  
  113. border-style: solid;  
  114. border-color: black;  
  115. background-color:white  
  116. }  
  117. </style>  
  118. "@ 
  119.  
  120. $body = @"  
  121. <p style="font-size:25px;family:calibri;color:#ff9100">  
  122. $TableHeader  
  123. </p>  
  124. "@  
  125.   
  126. $rptCollection | ConvertTo-HTML -head $tableStyle –body $body |Out-File c:\temp\ProfileReport-$defProf.htm