Deleting custom extended properties by using the EWS Managed API 2.0

Last modified: October 13, 2012

Applies to: EWS Managed API | Exchange Server 2007 Service Pack 1 (SP1) | Exchange Server 2010

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 Microsoft Exchange Web Services (EWS) Managed API 1.0 to delete extended properties on items and folders. Extended properties are custom properties on items and folders in an Exchange mailbox. Deleting an extended property involves removing the property from the item, updating the item, and saving the item to the Exchange mailbox. Removing the extended property does not remove the item from the Exchange database.

To delete an extended property

  1. Obtain the GUID that was used when the extended property was created.

  2. Define a view along with a page size. The following code shows an ItemView with a page size of 10.

    ItemView view = new ItemView(10);
    
  3. Create a GUID identifier from the GUID that was used when the extended property was created.

    Guid MyPropertySetId = new Guid("{C11FF724-AA03-4555-9952-8FA248A11C3E}");
    
  4. Create an ExtendedPropertyDefinition for the extended property.

    ExtendedPropertyDefinition extendedPropertyDefinition =   new ExtendedPropertyDefinition(MyPropertySetId, "Expiration Date", MapiPropertyType.String);
    
  5. Define the PropertySet to search for the defined extended property definition. Optionally, to enhance performance, limit the returned properties to the Id, Subject, and ExtendedPropertyDefinition properties.

    view.PropertySet = 
      new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, extendedPropertyDefinition);
    
  6. Retrieve a collection of all the mail to be searched for a user. In the following example, only the results of the Inbox are retrieved.

    FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, view);
    
  7. Instantiate an Item and an extended property index.

    Item message = null;
    int extendedPropertyindex = 0;
    
  8. Locate a specific item with an extended property. The following code example shows how to retrieve a collection of mail from the Exchange mailbox, including the extended property that is identified by the GUID. The code example searches for the first mail that is found that contains the defined extended property. It is the first message in the collection with the extended property that is to have the extended property removed.

    // Search the e-mail collection for the extended property.
    foreach (Item item in findResults.Items)
    {
        if (item.ExtendedProperties.Count > 0)
        {   // Retrieve only the first message with the defined extended property.
            // Identify the message, extended property index, and break.
            foreach (ExtendedProperty extendedProperty in item.ExtendedProperties)
            {
                if (extendedProperty.PropertyDefinition == extendedPropertyDefinition)
                {
                    message = item;
                    break;
                }
                extendedPropertyindex++;
            }
        }
    }
    
  9. Remove the extended property.

    message.RemoveExtendedProperty(extendedPropertyDefinition);
    
  10. Update the mailbox store.

    message.Update(ConflictResolutionMode.AlwaysOverwrite);
    

You can retrieve that message and search for or obtain the extended property. For more information, see Viewing custom extended properties by using the EWS Managed API 2.0.

Example

The following code example shows how to retrieve a collection of mail from the Exchange mailbox, including the extended property that is identified by the GUID. The code example searches each item and selects the first item that contains the defined extended property, removes the extended property, and updates the Exchange mailbox. This example assumes that at least one message with an extended property is in the user's mailbox. Note that only the extended property, and not the message, is deleted.

ItemView view = new ItemView(10);
// Get the GUID for the property set.
Guid MyPropertySetId = new Guid("{C11FF724-AA03-4555-9952-8FA248A11C3E}");

// Create a definition for the extended property.
ExtendedPropertyDefinition extendedPropertyDefinition = new ExtendedPropertyDefinition(MyPropertySetId, "Expiration Date", MapiPropertyType.String);

// Retrieve a collection of the first 10 mail items (as set by the ItemView) in the Inbox.
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, extendedPropertyDefinition);
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, view);

// Instantiate an Item and an index counter for subsequent retrieval.
Item message = null;
int extendedPropertyindex = 0;
// Search the e-mail collection for the extended property.
foreach (Item item in findResults.Items)
{
    if (item.ExtendedProperties.Count > 0)
    {   // Retrieve only the first item with the defined extended property.
        // Identify the item, extended property index, and break.
        foreach (ExtendedProperty extendedProperty in item.ExtendedProperties)
        {
            if (extendedProperty.PropertyDefinition == extendedPropertyDefinition)
            {
                message = item;
                break;
            }
            extendedPropertyindex++;
        }
    }
}
// Remove the extended property from the message.
message.RemoveExtendedProperty(extendedPropertyDefinition);
// Update the message.
message.Update(ConflictResolutionMode.AlwaysOverwrite);

The following example shows the XML request that is sent by the client to the server when an item with a deleted extended property is updated to an Exchange mailbox. The ItemId and ChangeKey attributes have been shortened to preserve readability.

<?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" />
  </soap:Header>
  <soap:Body>
    <m:UpdateItem MessageDisposition="SaveOnly" ConflictResolution="AlwaysOverwrite">
      <m:ItemChanges>
        <t:ItemChange>
          <t:ItemId Id="AAMkADBkO" ChangeKey="CQAAABYAAA" />
          <t:Updates>
            <t:DeleteItemField>
              <t:ExtendedFieldURI PropertySetId="c11ff724-aa03-4555-9952-8fa248a11c3e" PropertyName="Expiration Date" PropertyType="String" />
            </t:DeleteItemField>
          </t:Updates>
        </t:ItemChange>
      </m:ItemChanges>
    </m:UpdateItem>
  </soap:Body>
</soap:Envelope>

The following example shows the XML response that is returned by the server after it parses the request from the client. The ItemId and ChangeKey attributes have been shortened to preserve readability.

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:ServerVersionInfo MajorVersion="14" MinorVersion="0" MajorBuildNumber="499"  MinorBuildNumber="0" Version="Exchange2010"  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">
    <m:UpdateItemResponse xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
      <m:ResponseMessages>
        <m:UpdateItemResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:Items>
            <t:Message>
              <t:ItemId Id="AAMkADBkOD" ChangeKey="CQAAABYAAA" />
            </t:Message>
          </m:Items>
          <m:ConflictResults>
            <t:Count>0</t:Count>
          </m:ConflictResults>
        </m:UpdateItemResponseMessage>
      </m:ResponseMessages>
    </m:UpdateItemResponse>
  </s:Body>
</s:Envelope>

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.