Deleting Inbox rules by using the EWS Managed API 2.0
Last modified: October 13, 2012
Applies to: EWS Managed API | Exchange Server 2010 Service Pack 1 (SP1)
Note: This content applies to the EWS Managed API 2.0 and earlier versions. For the latest information about the EWS Managed API, see Web services in Exchange.
You can use the Exchange Web Services (EWS) Managed API to delete an existing Inbox rule from a user's mailbox in the Exchange store.
To delete an Inbox rule
Get the Inbox rule to be deleted 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 delete.
foreach (Rule ruleinCollection in ruleCollection) { if (ruleinCollection.DisplayName == "MoveInterestingToJunk") {
Create a DeleteRuleOperation object and set it to the rule identifier of the rule to be deleted.
DeleteRuleOperation deleteRuleOperation = new DeleteRuleOperation(ruleinCollection.Id);
Update the mailbox, which deletes the rule. The following example shows how to update the rules collection on the Exchange server.
service.UpdateInboxRules(new RuleOperation[] { deleteRuleOperation }, true); } }
Example
The following example shows how to delete an Inbox rule. In this example, the rule with a display name of "MoveInterestingToJunk" is deleted.
// Create a DeleteRuleOperation object.
// Get the RuleCollection.
// Delete the rule "MoveInterestingToJunk", if it exists.
RuleCollection ruleCollection = service.GetInboxRules("User1@Contoso.com ");
foreach (Rule ruleinCollection in ruleCollection)
{
if (ruleinCollection.DisplayName == "MoveInterestingToJunk")
{
DeleteRuleOperation deleteRuleOperation = new DeleteRuleOperation(ruleinCollection.Id);
service.UpdateInboxRules(new RuleOperation[] { deleteRuleOperation }, true);
}
}
The following example shows the XML request that is sent by the UpdateInboxRules method to delete an Inbox rule.
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types"
xmlns:soap="https://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:DeleteRuleOperation>
<t:RuleId>cSAAAABQVok=</t:RuleId>
</t:DeleteRuleOperation>
</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="https://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="14"
MinorVersion="1"
MajorBuildNumber="164"
MinorBuildNumber="0"
Version="Exchange2010_SP1"
xmlns:h="https://schemas.microsoft.com/exchange/services/2006/types"
xmlns="https://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="https://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.
The following example shows an invalid DeleteRuleOperation request that results in a RuleOperationError error response. In this example, the RuleId that is submitted in the DeleteRuleOperation request is invalid.
// Get the RuleCollection.
// Create the DeleteRuleOperation object.
RuleCollection ruleCollection = service.GetInboxRules("User1@Contoso.com");
foreach (Rule ruleinCollection in ruleCollection)
{
// The following line has an invalid RuleId, which causes the UpdateInboxRulesException to be thrown.
DeleteRuleOperation deleteRuleOperation = new DeleteRuleOperation("InvalidRuleIdString");
try
{
service.UpdateInboxRules(new RuleOperation[] { deleteRuleOperation }, true);
}
catch (UpdateInboxRulesException e)
{
// Display the property values for each RuleError.
foreach (RuleOperationError ruleOperationError in e.Errors)
{
foreach (RuleError ruleError in ruleOperationError)
{
Console.WriteLine(" ErrorCode: " + ruleError.ErrorCode);
Console.WriteLine("ErrorMessage: " + ruleError.ErrorMessage);
Console.WriteLine("RuleProperty: " + ruleError.RuleProperty);
Console.WriteLine(" Value: " + ruleError.Value);
}
}
}
}
The following example shows the XML request that is sent by the UpdateInboxRules method to delete an Inbox rule. The RuleId object in this example is invalid.
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types"
xmlns:soap="https://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:DeleteRuleOperation>
<t:RuleId>InvalidRuleIdString</t:RuleId>
</t:DeleteRuleOperation>
</m:Operations>
</m:UpdateInboxRules>
</soap:Body>
</soap:Envelope>
The following example shows the XML response that is returned by using the UpdateInboxRules method. Because the request has an invalid RuleId object, the RuleOperationErrors collection is returned.
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="14"
MinorVersion="1"
MajorBuildNumber="164"
MinorBuildNumber="0" Version="Exchange2010_SP1"
xmlns:h="https://schemas.microsoft.com/exchange/services/2006/types"
xmlns="https://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="Error"
xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
<MessageText>Validation error occurred during rule operation execution.</MessageText>
<ResponseCode>ErrorInboxRulesValidationError</ResponseCode>
<DescriptiveLinkKey>0</DescriptiveLinkKey>
<RuleOperationErrors>
<RuleOperationError xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
<OperationIndex>0</OperationIndex>
<ValidationErrors>
<Error>
<FieldURI>RuleId</FieldURI>
<ErrorCode>InvalidValue</ErrorCode>
<ErrorMessage>The value is invalid.</ErrorMessage>
<FieldValue>InvalidRuleIdString</FieldValue>
</Error>
</ValidationErrors>
</RuleOperationError>
</RuleOperationErrors>
</UpdateInboxRulesResponse>
</s:Body>
</s:Envelope>
This example assumes that the ExchangeService object is configured correctly to connect to the user's Client Access server.
Compiling the code
For information about compiling this code, see Getting started with the EWS Managed API 2.0.
Robust programming
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 2.0. 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 by using the EWS Managed API 2.0.
Security
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 by using the EWS Managed API 2.0.
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.