Muokkaa

Jaa


Work with Exchange mailbox items by using EWS in Exchange

Learn how to create, get, update, and delete items by using the EWS Managed API or EWS in Exchange.

You can use the EWS Managed API or EWS to work with items in a mailbox. You can use generic items — EWS Managed API Item objects or EWS Item types — to perform some operations (getting an item or deleting an item by using the item's identifier); however, most of the time you'll have to use a strongly typed item to perform a get or update operation because you'll need access to the properties that are specific to the strongly typed item.

For example, you can't use a generic item to retrieve an item that contains a start and end date - you need an EWS Managed API Appointment object or an EWS CalendarItem type to do that. And if you're using the EWS Managed API, you always have to create strongly typed items, because the generic Item class does not have a constructor. If you're working with an item that is not strongly typed, you can always use the base Item class to work with the item.

Table 1. EWS Managed API methods and EWS operations for working with items

In order to… EWS Managed API method EWS operation
Create a generic item
None. You can only create specific item types by using the EWS Managed API; you cannot create generic items.
CreateItem
Get an item
Item.Bind
GetItem
Update an item
Item.Update
UpdateItem
Delete an item
Item.Delete
DeleteItem

In this article, you'll learn when you can use the generic base class and when you need to use a strongly typed item to complete your task. The code examples will show you how to use the base class, and what to do when you can't use the base class or it doesn't fit your needs.

Create an item by using the EWS Managed API

The EWS Managed API does not have a publicly available constructor for the Item class, so you must use the constructor for the specific item type you want to create in order to create an item. For example, use the EmailMessage class constructor to create a new email message, and the Contact class constructor to create a new contact. Likewise, the server never returns generic Item objects in responses; all generic items are returned as EmailMessage objects.

When you know the type of item to create, you can complete the task in just a few steps. The steps are similar for all item types:

  1. Initialize a new instance of one of the Item classes with the ExchangeService object as a parameter.

  2. Set properties on the item. The schemas are different for each item type, so different properties are available for different items.

  3. Save the item, or save and send the item.

For example, you can create an EmailMessage object, set the Subject, Body, and ToRecipients properties, and then send it by using the EmailMessage.SendAndSaveCopy method.

// Create an email message and provide it with connection 
// configuration information by using an ExchangeService object named service.
EmailMessage message = new EmailMessage(service);
// Set properties on the email message.
message.Subject = "Company Soccer Team";
message.Body = "Are you interested in joining?";
message.ToRecipients.Add("sadie@contoso.com");
// Send the email message and save a copy.
// This method call results in a CreateItem call to EWS.
message.SendAndSaveCopy();

To learn how to create a meeting or appointment item by using the EWS Managed API, see Create appointments and meetings by using EWS in Exchange 2013.

Create an item by using EWS

You can create a generic item or a strongly typed item by using EWS. The steps are similar for all item types:

  1. Use the CreateItem operation to create an item in the Exchange store.

  2. Use the Items element to contain one or more items to create.

  3. Set properties on the item.

For example, you can create an email message and send it by using the code in the following example. This is also the XML request that the EWS Managed API sends when you call the SendAndSaveCopy method.

<?xml version="1.0" encoding="utf-8"?>
<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="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2007_SP1" />
  </soap:Header>
  <soap:Body>
    <m:CreateItem MessageDisposition="SendAndSaveCopy">
      <m:SavedItemFolderId>
        <t:DistinguishedFolderId Id="sentitems" />
      </m:SavedItemFolderId>
      <m:Items>
        <t:Message>
          <t:Subject>Company Soccer Team</t:Subject>
          <t:Body BodyType="HTML">Are you interested in joining?</t:Body>
          <t:ToRecipients>
            <t:Mailbox>
              <t:EmailAddress>sadie@contoso.com </t:EmailAddress>
              </t:Mailbox>
          </t:ToRecipients>
        </t:Message>
      </m:Items>
    </m:CreateItem>
  </soap:Body>
</soap:Envelope>

The server responds to the CreateItem request with a CreateItemResponse message that includes a ResponseCode value of NoError, which indicates that the email was created successfully, and the ItemId of the newly created message.

To learn how to create a meeting or appointment item by using EWS, see Create appointments and meetings by using EWS in Exchange 2013.

Get an item by using the EWS Managed API

To use the EWS Managed API to get an item if you know the Item.Id of the item to retrieve, you simply call one of the Bind methods on the item, and the item is retrieved. As a best practice, we recommend that you limit the properties returned to only those that are required. This example returns the item Id property and the Subject property.

This example assumes that service is a valid ExchangeService object and that the user has been authenticated to an Exchange server. The local variable itemId is the Id of the item to update.

// As a best practice, limit the properties returned to only those that are required.
PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject);
// Bind to the existing item by using the ItemId.
// This method call results in a GetItem call to EWS.
Item item = Item.Bind(service, itemId, propSet);

If you're searching for an item that meets specific criteria, do the following:

  1. Bind to the folder that contains the items to get.

  2. Instantiate a SearchFilter.SearchFilterCollection or a PropertySet to filter the items to return.

  3. Instantiate an ItemView or CalendarView object to specify the number of items to return.

  4. Call the ExchangeService.FindItems or ExchangeService.FindAppointments method.

For example, if you want to retrieve unread email messages in the Inbox, use the code in the following example. This example uses a SearchFilterCollection to limit the results of the FindItems method to unread messages, and limits the ItemView to limit results to one item. This particular code only works on EmailMessage objects because the EmailMessageSchema.IsRead value is part of the SearchFilter.

// Bind the Inbox folder to the service object.
Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);
// The search filter to get unread email.
SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
ItemView view = new ItemView(1);
// Fire the query for the unread items.
// This method call results in a FindItem call to EWS.
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, sf, view);

Alternatively, you can use a PropertySet to limit the results of the search as shown in the following code example. This example uses the FindAppointments method to retrieve up to five appointments that occur in the next 30 days. This code of course only works on calendar items.

// Initialize values for the start and end times, and the number of appointments to retrieve.
DateTime startDate = DateTime.Now;
DateTime endDate = startDate.AddDays(30);
const int NUM_APPTS = 5;
// Bind the Calendar folder to the service object.
// This method call results in a GetFolder call to EWS.
CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet());
// Set the start and end time and number of appointments to retrieve.
CalendarView cView = new CalendarView(startDate, endDate, NUM_APPTS);
// Limit the properties returned to the appointment's subject, start time, and end time.
cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);
// Retrieve a collection of appointments by using the calendar view.
// This method call results in a FindAppointments call to EWS.
FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);

Note that the information the server returns in the Bind method response is different than the information that the server returns for a FindItem or FindAppointment method response. The Bind method can return all the schematized properties, whereas the FindItem and FindAppointment methods do not return all the schematized properties. So if you need full access to the item, you'll have to use the Bind method. If you don't have the item Id of the item you'd like to retrieve, use the FindItem or FindAppointment methods to retrieve the Id, and then use the Bind method to retrieve the properties you need.

To learn how to get a meeting or appointment item by using the EWS Managed API, see Get appointments and meetings by using EWS in Exchange.

Get an item by using EWS

If you know the ItemId of the item to retrieve, you can get the item by using the GetItem operation.

The following example shows the XML request to get the Subject of an item with a specific ItemId. This is also the XML request that the EWS Managed API sends when calling the Bind method on an ItemId. The values of some attributes and elements have been shortened for readability.

<?xml version="1.0" encoding="utf-8"?>
<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="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2013" />
  </soap:Header>
  <soap:Body>
    <m:GetItem>
      <m:ItemShape>
        <t:BaseShape>IdOnly</t:BaseShape>
        <t:AdditionalProperties>
          <t:FieldURI FieldURI="item:Subject" />
        </t:AdditionalProperties>
      </m:ItemShape>
      <m:ItemIds>
        <t:ItemId Id="GJc/NAAA=" />
      </m:ItemIds>
    </m:GetItem>
  </soap:Body>
</soap:Envelope>

The following example shows the XML response that the server returns after it processes the GetItem operation. The response indicates the item was retrieved successfully. The values of some attributes and elements have been shortened for readability.

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:ServerVersionInfo MajorVersion="15" 
                         MinorVersion="0" 
                         MajorBuildNumber="815" 
                         MinorBuildNumber="6" 
                         Version="V2_7" 
                         xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" 
                         xmlns="http://schemas.microsoft.com/exchange/services/2006/types" 
                         xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
  </s:Header>
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <m:GetItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" 
                       xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
      <m:ResponseMessages>
        <m:GetItemResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:Items>
            <t:Message>
              <t:ItemId Id="GJc/NAAA=" ChangeKey="CQAAABYAAAAPxolXAHv3TaHUnjW8wWqXAAAGJd9Z"/>
              <t:Subject>Company Soccer Team</t:Subject>
            </t:Message>
          </m:Items>
        </m:GetItemResponseMessage>
      </m:ResponseMessages>
    </m:GetItemResponse>
  </s:Body>
</s:Envelope>

If you do not know the ItemId of the item you want to retrieve, you can use the FindItem operation to find the item. In order to use the FindItem operation, you must first identify the folder that you're searching. You can identify the folder by using its DistinguinguishedFolderName or by using the FolderId. You can use either the FindFolder or SyncFolderHierarchy operations to get the FolderId you need. Then use the FindItem operation to search that folder for results that match the search filter. Unlike the EWS Managed API, EWS does not provide a separate find operation for appointments. The FindItem operation retrieves items of all types.

The following example shows the XML FindItem operation request that is sent to the server to find appointments in the Calendar folder that occur in the next 30 days. The values of some attributes and elements have been shortened for readability.

<?xml version="1.0" encoding="utf-8"?>
<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="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2007_SP1" />
  </soap:Header>
  <soap:Body>
    <m:FindItem Traversal="Shallow">
      <m:ItemShape>
        <t:BaseShape>IdOnly</t:BaseShape>
        <t:AdditionalProperties>
          <t:FieldURI FieldURI="item:Subject" />
          <t:FieldURI FieldURI="calendar:Start" />
          <t:FieldURI FieldURI="calendar:End" />
        </t:AdditionalProperties>
      </m:ItemShape>
      <m:CalendarView MaxEntriesReturned="5" StartDate="2013-10-16T17:04:28.722Z" EndDate="2013-11-15T18:04:28.722Z" />
      <m:ParentFolderIds>
        <t:FolderId Id="AAAEOAAA=" ChangeKey="AgAAABYAAAAqRr3mNdNMSasqx/o9J13UAAAAAAA3" />
      </m:ParentFolderIds>
    </m:FindItem>
  </soap:Body>
</soap:Envelope>

The server responds to the FindItem request with a FindItemResponse message that includes the ResponseCode value of NoError, which indicates that the operation completed successfully. If any calendar items meet the filtering criteria, they are included in the response.

Note that the information the server returns in the GetItem operation response is different than the information the server returns in a FindItem or FindAppointment operation response. The GetItem operation can return all the schematized properties, whereas the FindItem and FindAppointment operations do not return all the schematized properties. So if you need full access to the item, you'll have to use the GetItem operation. If you don't have the ItemId of the item you'd like to retrieve, use the FindItem or FindAppointment operations to retrieve the ItemId, and then use the GetItem operation to retrieve the elements you need.

To learn how to get a meeting or appointment item by using EWS, see Get appointments and meetings by using EWS in Exchange.

Update an item by using the EWS Managed API

The steps to update an item by using the EWS Managed API are similar for all item types; however, the item properties are different for each item type, and the Update method has many overloaded methods to choose from. To update an item:

  1. Use the Bind method to get the latest version of the item, unless you already have it. To update properties specific to a strongly typed item, you'll have to bind to that item type. To update properties available on the generic item type, you can bind to the Item object.

  2. Update the properties on the item.

  3. Call the Update method.

For example, you can update the subject of an email by using the generic item type, as shown in the code in the following example.

This example assumes that service is a valid ExchangeService object and that the user has been authenticated to an Exchange server. The local variable itemId is the Id of the item to update.

// Bind to the existing item, using the ItemId.
// This method call results in a GetItem call to EWS.
Item item = Item.Bind(service, itemId);
// Update the Subject of the email.
item.Subject = "New subject";
// Save the updated email.
// This method call results in an UpdateItem call to EWS.
item.Update(ConflictResolutionMode.AlwaysOverwrite);

To learn how to update a meeting or appointment item by using the EWS Managed API, see Update appointments and meetings by using EWS in Exchange.

Update an item by using EWS

To update an item by using EWS, do the following:

  1. Use the GetItem operation to get the latest version of the item, unless you already have it.

  2. Use the UpdateItem operation to specify fields to update and assign new values to those fields.

The following example shows the XML UpdateItem operation request that is sent to the server to update the Subject value of the email message. The values of some attributes and elements have been shortened for readability.

<?xml version="1.0" encoding="utf-8"?>
<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="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2010_SP1" />
  </soap:Header>
  <soap:Body>
    <m:UpdateItem MessageDisposition="SaveOnly" ConflictResolution="AlwaysOverwrite">
      <m:ItemChanges>
        <t:ItemChange>
          <t:ItemId Id="APdZjAAA=" ChangeKey="CQAAABYAAAAqRr3mNdNMSasqx/o9J13UAAAAPdgr" />
          <t:Updates>
            <t:SetItemField>
              <t:FieldURI FieldURI="item:Subject" />
              <t:Message>
                <t:Subject>New subject</t:Subject>
              </t:Message>
            </t:SetItemField>
          </t:Updates>
        </t:ItemChange>
      </m:ItemChanges>
    </m:UpdateItem>
  </soap:Body>
</soap:Envelope>

The server responds to the UpdateItem request with a UpdateItemResponse message that includes the a ResponseCode value of NoError, which indicates that the item update was successful.

To learn how to update a meeting or appointment item by using EWS, see Update appointments and meetings by using EWS in Exchange.

Delete an item by using the EWS Managed API

You can delete items by moving them to the Deleted Items folder or to the dumpster. If you know the ItemId of the item to delete, just call the Delete method on the item.

If you need to find the item before deleting it, do the following:

  1. Call the FindItems or FindAppointments method to find the item to delete.

  2. Instantiate a PropertySet and limit it to the properties to return, or use a SearchFilterCollection to find specific items.

  3. Instantiate an ItemView or CalendarView to specify the number of items to return.

  4. Call the Delete method.

For example, the following code shows how to move an email message to the Deleted Items folder.

This example assumes that service is a valid ExchangeService object and that the user has been authenticated to an Exchange server. The local variable itemId is the Id of the item to update.

// Bind to the existing item, using the ItemId.
// This method call results in a GetItem call to EWS.
Item item = Item.Bind(service, itemId);
// Delete the item by moving it to the Deleted Items folder.
// This method call results in a DeleteItem call to EWS.
item.Delete(DeleteMode.MoveToDeletedItems);

For more details about deleting items, see Deleting items by using EWS in Exchange. To learn how to delete a meeting or appointment item by using the EWS Managed API, see Delete appointments and cancel meetings by using EWS in Exchange.

Delete an item by using EWS

You can delete an item by using the DeleteItem operation.

The following example shows the XML request that is sent to the server to move the email message to the Deleted Items folder. The values of some attributes and elements have been shortened for readability.

<?xml version="1.0" encoding="utf-8"?>
<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="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2010_SP1" />
  </soap:Header>
  <soap:Body>
    <m:DeleteItem DeleteType="MoveToDeletedItems">
      <m:ItemIds>
        <t:ItemId Id="APdZjAAA=" ChangeKey="CQAAABYAAAAqRr3mNdNMSasqx/o9J13UAAANIFzC" />
      </m:ItemIds>
    </m:DeleteItem>
  </soap:Body>
</soap:Envelope>

The server responds to the DeleteItem request with a DeleteItemResponse message that includes the a ResponseCode value of NoError, which indicates that the item deletion was successful.

For more details about deleting items, see Deleting items by using EWS in Exchange. To learn how to delete a meeting or appointment item by using EWS, see Delete appointments and cancel meetings by using EWS in Exchange.

Move or copy items to another mailbox

You can move or copy items between mailboxes by using the ExportItems and UploadItems operations. To learn more, see Exporting and importing items by using EWS in Exchange.

See also