Updating Inbox rules by using the EWS Managed API
Published: July 16, 2012
Applies to: Exchange 2013 | Exchange Online | Exchange Server 2010 Service Pack 1 (SP1) | Exchange Web Services (EWS) Managed API
You can use the Microsoft Exchange Web Services (EWS) Managed API to update an existing Inbox rule from a user's mailbox in the Exchange store.
To update an Inbox rule
Get the Inbox rule to be modified by calling the GetInboxRules method to retrieve a collection of all server rules for the user. You must provide it with connection configuration information by using an ExchangeService object named service, as shown in the following example.
RuleCollection ruleCollection = service.GetInboxRules("User1@Contoso.com");Loop through the entire rule collection, searching for the rule to update.
foreach (Rule ruleinCollection in ruleCollection) { if (ruleinCollection.DisplayName == "MoveInterestingToJunk") {
Modify the rule. In this example, the Conditions for the rule is modified to contain the string "This is Junk".
ruleinCollection.Conditions.ContainsSubjectStrings.Clear(); ruleinCollection.Conditions.ContainsSubjectStrings.Add("This is Junk");Create a SetRuleOperation object.
SetRuleOperation setRuleOperation = new SetRuleOperation(ruleinCollection);Update the mailbox, which updates the rule. The following example shows how to update the rules collection on the Exchange server.
service.UpdateInboxRules(new RuleOperation[] { setRuleOperation }, true); } }
The following example shows how to update an Inbox rule. In this example, the rule with a DisplayName of "MoveInterestingToJunk" is updated. The conditions that, when fulfilled, will trigger the rule actions are modified to require that the subject contain the string "This is Junk".
// Modify the "MoveInterestingToJunk" rule, if it exists. // Get the RuleCollection. RuleCollection ruleCollection = service.GetInboxRules("User1@Contoso.com"); foreach (Rule ruleinCollection in ruleCollection) { if (ruleinCollection.DisplayName == "MoveInterestingToJunk") { ruleinCollection.Conditions.ContainsSubjectStrings.Clear(); ruleinCollection.Conditions.ContainsSubjectStrings.Add("This is Junk"); SetRuleOperation setRuleOperation = new SetRuleOperation(ruleinCollection); service.UpdateInboxRules(new RuleOperation[] { setRuleOperation }, true); } }
The following example shows the XML request that is sent by the UpdateInboxRules method to modify an Inbox rule. The FolderId attribute was shortened to preserve readability.
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<t:RequestServerVersion Version="Exchange2010_SP1" />
</soap:Header>
<soap:Body>
<m:UpdateInboxRules>
<m:RemoveOutlookRuleBlob>true</m:RemoveOutlookRuleBlob>
<m:Operations>
<t:SetRuleOperation>
<t:Rule>
<t:RuleId>cSAAAABQVo8=</t:RuleId>
<t:DisplayName>MoveInterestingToJunk</t:DisplayName>
<t:Priority>1</t:Priority>
<t:IsEnabled>true</t:IsEnabled>
<t:IsInError>false</t:IsInError>
<t:Conditions>
<t:ContainsSubjectStrings>
<t:String>This is Junk</t:String>
</t:ContainsSubjectStrings>
</t:Conditions>
<t:Exceptions />
<t:Actions>
<t:MoveToFolder>
<t:FolderId Id="AQMkADhhOGU0" ChangeKey="AQAAAA==" />
</t:MoveToFolder>
</t:Actions>
</t:Rule>
</t:SetRuleOperation>
</m:Operations>
</m:UpdateInboxRules>
</soap:Body>
</soap:Envelope>
The following example shows the XML response that is returned by using the UpdateInboxRules method.
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="14"
MinorVersion="1"
MajorBuildNumber="164"
MinorBuildNumber="0"
Version="Exchange2010_SP1"
xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types"
xmlns="http://schemas.microsoft.com/exchange/services/2006/types"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UpdateInboxRulesResponse ResponseClass="Success"
xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
<ResponseCode>NoError</ResponseCode>
</UpdateInboxRulesResponse>
</s:Body>
</s:Envelope>
This example assumes that the ExchangeService object is configured correctly to connect to the user’s Client Access server.
For information about compiling this code, see Getting started with the EWS Managed API.
Write appropriate error handling code for common search errors.
Review the client request XML that is sent to the Exchange server.
Review the server response XML that is sent from the Exchange server.
Set the service binding as shown in Setting the Exchange service URL by using the EWS Managed API. Do not hard code URLs because if mailboxes move, they might be serviced by a different Client Access server. If the client cannot connect to the service, retry setting the binding by using the AutodiscoverUrl(String) method.
Set the target Exchange Web Services schema version by setting the requestedServerVersion parameter of the ExchangeService constructor. For more information, see Versioning EWS requests.
Use HTTP with SSL for all communication between client and server.
Always validate the server certificate that is used for establishing the SSL connections. For more information, see Validating X509 certificates.
Do not include user names and passwords in trace files.
Verify that Autodiscover lookups that use HTTP GET to find an endpoint always prompt for user confirmation; otherwise, they should be blocked.
Date | Description |
|---|---|
July 16, 2012 | Initial publication |