Partager via


Creating 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 to add extended properties to items and folders. Extended properties are custom properties on items and folders in an Exchange mailbox.

To create an extended property

  1. Define the extended property. In the following example, the property is an "Expiration Date" with a property type of a string. A GUID is used as the property set identifier.

    // 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);
    
  2. Add the extended property to the item. In this example, the extended property is added to a message item.

    message.SetExtendedProperty(extendedPropertyDefinition, DateTime.Now.AddDays(2).ToString());
    
  3. Save the item to the Exchange mailbox.

After you create an extended property and attach it to an item, you can search on that property in the Exchange mailbox. For more information, see Viewing custom extended properties by using the EWS Managed API 2.0.

Example

The following code example shows how to create a new extended property, set a value for the extended property, and add it to a message.

// 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);

// Create an e-mail message that you will add the extended property to.
EmailMessage message = new EmailMessage(service);
message.Subject = "Saved with extendedPropertyDefinition of two days";
message.Body = "The expiration date is contained within the extended property.";
message.ToRecipients.Add("user@contoso.com");

// Add the extended property to an e-mail message object named "message".
message.SetExtendedProperty(extendedPropertyDefinition, DateTime.Now.AddDays(2).ToString());

// Save the e-mail message.
message.SendAndSaveCopy();
' Get the GUID for the property set.
Dim MyPropertySetId As New Guid("{C11FF724-AA03-4555-9952-8FA248A11C3E}")

' Create a definition for the extended property.
Dim extendedPropertyDefinition As New ExtendedPropertyDefinition(MyPropertySetId, "Expiration Date", MapiPropertyType.String)

' Create an e-mail message that you will add the extended property to.
Dim message As New EmailMessage(service)
message.Subject = "Saved with extendedPropertyDefinition of two days"
message.Body = "The expiration date is contained within the extended property."
message.ToRecipients.Add("user@contoso.com")

' Add the extended property to an e-mail message object named "message".
message.SetExtendedProperty(extendedPropertyDefinition, DateTime.Now.AddDays(2).ToString)

' Save the e-mail message.
message.SendAndSaveCopy()

The following example shows the XML request that is sent by the client to the server when an extended property is saved to the Exchange database.

<?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:CreateItem MessageDisposition="SendAndSaveCopy">
      <m:SavedItemFolderId>
        <t:DistinguishedFolderId Id="sentitems" />
      </m:SavedItemFolderId>
      <m:Items>
        <t:Message>
          <t:Subject>Saved with extendedPropertyDefinition of two days</t:Subject>
          <t:Body BodyType="Text">The expiration date is contained within the extended property.</t:Body>
          <t:ExtendedProperty>
            <t:ExtendedFieldURI PropertySetId="c11ff724-aa03-4555-9952-8fa248a11c3e"
              PropertyName="Expiration Date" PropertyType="String" />
            <t:Value>1/11/2009 11:34:33 AM</t:Value>
          </t:ExtendedProperty>
          <t:ToRecipients>
            <t:Mailbox>
              <t:EmailAddress>user@contoso.com</t:EmailAddress>
            </t:Mailbox>
          </t:ToRecipients>
        </t:Message>
      </m:Items>
    </m:CreateItem>
  </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.

<?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:CreateItemResponse xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages"
        xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
      <m:ResponseMessages>
        <m:CreateItemResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:Items />
        </m:CreateItemResponseMessage>
      </m:ResponseMessages>
    </m:CreateItemResponse>
  </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.