Saturday, December 12, 2009

Changing OWA configuration setting with EWS in Exchange 2010

The ability to change and customise the default configuration of OWA is an often sort out and in previous versions of Exchange hard to achieve task. Especially for those with Large Exchange Orgs and standardised training or custom security requirements where the default settings come into conflict with other policies. Underlying a lot of OWA configuration settings are stored in FAI items (folder associated items) in a users mailbox using the Configuration Information Protocol Specification documented in http://msdn.microsoft.com/en-us/library/cc463899(EXCHG.80).aspx.

Along with the ability to access FAI items which was previously not possible in Exchange 2007 using EWS, Exchange 2010 also introduces a new UserConfiguration Operation in EWS that provides some Typed objects the makes dealing with configuration objects a lot easier and safer for those that wish to do so. (I still wouldn't guarantee the support response you would get if you do use these Operations and stuff your Exchange Org completely this is a risk you should understand before undertaking such an endeavor). What having these strong objects to use if your using Proxy code or the EWS Managed API means is that a task that may have taken a lot effort and reverse engineering in the past to achieve is really now something you can do fairly easy with a couple of lines of code or script.

The User configuration FAI items can be located in different folders in a Mailbox the one im going to focus on in this post is the OWA.UserOptions object which contain things such as the readingpane setting, OWA signiture and other general settings. The message itself has a message class of IPM.Configuration.OWA.UserOptions if you looked at it with a Mapi editor and its located in the Non_IPM_Subtree (ed Note since the removal of Dav in Exchange 2010 is this still the right terminology??).

This brings up an important point for anybody trying to modify these type of objects one tool you will find invaluable is a Mapi editory like the MFCMapi or OutlookSpy. Another useful tool is now the EWS editor these two tools will make doing this type of thing a lot eaiser as they can allow you to browse and see the current values of properties on items. You should also read the the protocol document i linked above and have an understanding of the datatypes involved with the properties your setting and make sure you adhere to this. Eg dont use a String where a Interger is expected etc.

For this post im also using the EWS Managed API which saves time and sanity when writing EWS code.

Lets get down to the code the first thing you need to do is get the FolderID for the Non_Ipm_Subtee, to do this you Bind to the Root folder and use the parentFolderID property

Folder Root = Folder.Bind(service, WellKnownFolderName.Root);

Next you use the UserConfiguration objects bind method and pass the following parameters in

UserConfiguration OWAConfig = UserConfiguration.Bind(service, "OWA.UserOptions", Root.ParentFolderId, UserConfigurationProperties.All);

Note that you take the MessageClass of the Item IPM.Configuration.OWA.UserOptions and drop the IPM.Configuration part.

This will then get the existing configuration object settings from this particular folder item which for this item will be returned as a dictionary collection. If you want to enum through all the properties you can do the following

IDictionaryEnumerator ConfigEnum = (IDictionaryEnumerator)OWAConfig.Dictionary.GetEnumerator();
while (ConfigEnum.MoveNext()) {
Console.WriteLine("Key : " + ConfigEnum.Key);
Console.WriteLine("Value : " + ConfigEnum.Value);
}


To Set a value it pretty simple eg

if(OWAConfig.Dictionary.ContainsKey("previewmarkasread")){
OWAConfig.Dictionary["previewmarkasread"] = 2;
}
else{
OWAConfig.Dictionary.Add("previewmarkasread", 2);
}
OWAConfig.Update();

This turns the Mark as Read off in the previewpane in OWA note that this is a Intager value.

To do this in Powershell script that you could feed with get-mailbox it would look like the following I've put a download of the code here

$MailboxName = "user@domain.com"

$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.0\Microsoft.Exchange.WebServices.dll"
[void][Reflection.Assembly]::LoadFile($dllpath)
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010)

$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">"
$aceuser = [ADSI]$sidbind

$service.AutodiscoverUrl($mailboxname)

$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$MailboxName)
$Root = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
$OWAConfig = [Microsoft.Exchange.WebServices.Data.UserConfiguration]::Bind($service, "OWA.UserOptions", $Root.ParentFolderId, [Microsoft.Exchange.WebServices.Data.UserConfigurationProperties]::All)
if($OWAConfig.Dictionary.ContainsKey("previewmarkasread")){
$OWAConfig.Dictionary["previewmarkasread"] = 2 }
else{
$OWAConfig.Dictionary.Add("previewmarkasread", 2)
}
$OWAConfig.Update()
"Done"

4 comments:

Will said...

Hello Glen, thanks for this post it was really useful. I've been trying to set the readreceipt option to 1 (always reply) and it works well but only if the mailbox has already been logged into. If the mailbox is new then the FAI item does not exist yet and the code errors. Do you know of a way of creating this item first in pwershell so I can set the default behaviour for new mailboxes before the user logs on? Seems that the first log on page which sets language and timezone creates this item. Thanks for any advice.

Glen said...

You would need to use Create Item with all the correct settings which could be tricky to get correct.

Cheers
Glen

Pham Ngoc Dai said...
This comment has been removed by the author.
Pham Ngoc Dai said...

Hi Glen,
Can I ask a question if it is possible for getting these user options using WebDAV? if YES, can you post the code?