Rebex EWS

EWS, SMTP, MIME, S/MIME libraries for .NET

Download 30-day free trial Buy from $199
More .NET libraries

Back to feature list...

Item operations

More item operations:


The sample code on this page assumes you have already connected and authenticated to an Exchange server.

Items in EWS 

EWS supports various item types in addition to e-mail messages. These include contacts, appointments, tasks, and so on. Please note that a folder is not an item, but rather a container that stores various item types.

In Rebex EWS, an EWS item is represented by EwsItemInfo class. Its item type can be determined using EwsItemInfo.ItemType property. If an item is a message, you can cast the EwsItemInfo to EwsMessageInfo object which provides additional information specific to message items.

Note: Specialized classes for non-message item types are not available yet, but you can easily access raw item data.

Tip: for more information about messages, see Message operations.

Getting list of items 

To get a list of items, use GetItemList method. The following code lists first 25 contact items:

// create EWS client instance, connect, log in
var ews = new Rebex.Net.Ews();
ews.Connect(hostname);
ews.Login(username, password);

// get list of first 25 contacts in Contacs folder
EwsItemCollection items = ews.GetItemList(
    EwsFolderId.Contacts,
    EwsItemFields.Full,
    EwsPageView.CreateIndexed(0, 25));

// print info about contacts found
foreach (EwsItemInfo item in items)
{
    Console.WriteLine("Contact ID: {0}", item.Id);
    Console.WriteLine("Contact Name: {0}", item.Subject);
}
' create EWS client instance, connect, log in
Dim ews As New Rebex.Net.Ews()
ews.Connect(hostname)
ews.Login(username, password)

' get list of first 25 contacts in Contacts folder
Dim items As EwsItemCollection = ews.GetItemList(
    EwsFolderId.Contacts,
    EwsItemFields.Full,
    EwsPageView.CreateIndexed(0, 25))

' print info about contacts found
For Each item As EwsItemInfo In items
    Console.WriteLine("Contact ID: {0}", item.Id)
    Console.WriteLine("Contact Name: {0}", item.Subject)
Next

Tip: GetItemList methods is equivalent to GetMessageList method - see its description to learn about paging or how to specify fields to retrieve.

Getting item info 

To get information about an item, use GetItemInfo method.

Tip: GetItemInfo methods is equivalent to GetMessageInfo method - see its description to learn how to specify fields to retrieve.

Getting XML representation 

The EwsItemInfo object doesn't offer properties to access all values provided by the Exchange server. Use the ToXml method to retrieve the whole raw SOAP XML response from the Exchange server and load it into XmlDocument to work with it:

// create EWS client instance, connect, log in
// ...

// get full info about a contact
EwsItemInfo item = ews.GetItemInfo(contactId, EwsItemFields.Full);

// get the XML representation of the item
string xml = item.ToXml();

// prepare XmlDocument for XPath selection
XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(xml);
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("ns", "http://schemas.microsoft.com/exchange/services/2006/types");

// print contact name and e-mail addresses
Console.WriteLine("Contact Name: {0}", item.Subject);
foreach (XmlNode node in doc.SelectNodes("//ns:EmailAddresses/ns:Entry", ns))
{
    Console.WriteLine("Contact address: {0}", node.InnerText);
}
' create EWS client instance, connect, log in
' ...

' get full info about a contact
Dim item As EwsItemInfo = ews.GetItemInfo(contactId, EwsItemFields.Full)

' get the XML representation of the item
Dim xml As String = item.ToXml()

' prepare XmlDocument for XPath selection
Dim doc As New System.Xml.XmlDocument()
doc.LoadXml(xml)
Dim ns As New XmlNamespaceManager(doc.NameTable)
ns.AddNamespace("ns", "http://schemas.microsoft.com/exchange/services/2006/types")

' print contact name and e-mail addresses
Console.WriteLine("Contact Name: {0}", item.Subject)
For Each node As XmlNode In doc.SelectNodes("//ns:EmailAddresses/ns:Entry", ns)
    Console.WriteLine("Contact address: {0}", node.InnerText)
Next

Downloading items 

To download an item into a file or a stream, use GetItem method. or EwsItemFormat.Xml to download the item in native Exchange export format.

Searching for items 

Use SearchItems method to search for items matching the specified criteria. Check out Searching for more information.

Creating items from XML 

To create an item that is not a mail message, use EwsItemInfo.FromXml method. The format of source XML for various item types can be found on MSDN website:

Use the following code to create a Task item:

            string sourceXml =
@"<t:Task xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">
  <t:ItemClass>IPM.Task</t:ItemClass>
  <t:Subject>Stored task</t:Subject>
  <t:Sensitivity>Normal</t:Sensitivity>
  <t:Body BodyType=""HTML"">&lt;html&gt; &lt;head&gt; &lt;meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8""&gt; &lt;/head&gt; &lt;body&gt; Test task &lt;strong&gt;BODY&lt;/strong&gt; &lt;/body&gt; &lt;/html&gt; </t:Body>
  <t:Importance>Low</t:Importance>
  <t:Culture>en-US</t:Culture>
  <t:PercentComplete>0</t:PercentComplete>
  <t:Status>NotStarted</t:Status>
</t:Task>";

            EwsItemInfo taskInfo = EwsItemInfo.FromXml(sourceXml);
            EwsItemId taskId = ews.StoreItem(EwsFolderId.Tasks, taskInfo);
        Dim sourceXml As String =
"<t:Task xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">
  <t:ItemClass>IPM.Task</t:ItemClass>
  <t:Subject>Stored task</t:Subject>
  <t:Sensitivity>Normal</t:Sensitivity>
  <t:Body BodyType=""HTML"">&lt;html&gt; &lt;head&gt; &lt;meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8""&gt; &lt;/head&gt; &lt;body&gt; Test task &lt;strong&gt;BODY&lt;/strong&gt; &lt;/body&gt; &lt;/html&gt; </t:Body>
  <t:Importance>Low</t:Importance>
  <t:Culture>en-US</t:Culture>
  <t:PercentComplete>0</t:PercentComplete>
  <t:Status>NotStarted</t:Status>
</t:Task>"

        Dim taskInfo As EwsItemInfo = EwsItemInfo.FromXml(sourceXml)
        Dim taskId As EwsItemId = ews.StoreItem(EwsFolderId.Tasks, taskInfo)

Use the following code to create a Calendar item:

            string sourceXml =
@"<t:CalendarItem xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">
  <t:ItemClass>IPM.Appointment</t:ItemClass>
  <t:Subject>Homeoffice</t:Subject>
  <t:Sensitivity>Normal</t:Sensitivity>
  <t:Importance>Normal</t:Importance>
  <t:ReminderDueBy>2016-12-09T00:00:00+02:00</t:ReminderDueBy>
  <t:ReminderIsSet>false</t:ReminderIsSet>
  <t:ReminderMinutesBeforeStart>1080</t:ReminderMinutesBeforeStart>
  <t:Culture>cs-CZ</t:Culture>
  <t:UID>040000008200E00074C5B7101A82E00800000000F08D092BB4B6D10100000000000000001000000044961234071B4748B2696AC3BFE8B3FA</t:UID>
  <t:Start>2016-12-12T00:00:00+02:00</t:Start>
  <t:End>2016-12-13T00:00:00+02:00</t:End>
  <t:IsAllDayEvent>true</t:IsAllDayEvent>
  <t:LegacyFreeBusyStatus>Free</t:LegacyFreeBusyStatus>
  <t:IsResponseRequested>true</t:IsResponseRequested>
  <t:ConferenceType>0</t:ConferenceType>
  <t:AllowNewTimeProposal>true</t:AllowNewTimeProposal>
</t:CalendarItem>";

            EwsItemInfo calendarInfo = EwsItemInfo.FromXml(sourceXml);
            EwsItemId calendarId = ews.StoreItem(EwsFolderId.Calendar, calendarInfo);
        Dim sourceXml As String =
"<t:CalendarItem xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">
  <t:ItemClass>IPM.Appointment</t:ItemClass>
  <t:Subject>Homeoffice</t:Subject>
  <t:Sensitivity>Normal</t:Sensitivity>
  <t:Importance>Normal</t:Importance>
  <t:ReminderDueBy>2016-12-09T00:00:00+02:00</t:ReminderDueBy>
  <t:ReminderIsSet>false</t:ReminderIsSet>
  <t:ReminderMinutesBeforeStart>1080</t:ReminderMinutesBeforeStart>
  <t:Culture>cs-CZ</t:Culture>
  <t:UID>040000008200E00074C5B7101A82E00800000000F08D092BB4B6D10100000000000000001000000044961234071B4748B2696AC3BFE8B3FA</t:UID>
  <t:Start>2016-12-12T00:00:00+02:00</t:Start>
  <t:End>2016-12-13T00:00:00+02:00</t:End>
  <t:IsAllDayEvent>true</t:IsAllDayEvent>
  <t:LegacyFreeBusyStatus>Free</t:LegacyFreeBusyStatus>
  <t:IsResponseRequested>true</t:IsResponseRequested>
  <t:ConferenceType>0</t:ConferenceType>
  <t:AllowNewTimeProposal>true</t:AllowNewTimeProposal>
</t:CalendarItem>"

        Dim calendarInfo As EwsItemInfo = EwsItemInfo.FromXml(sourceXml)
        Dim calendarId As EwsItemId = ews.StoreItem(EwsFolderId.Calendar, calendarInfo)

Use the following code to create a Conatact item:

            string sourceXml =
@"<t:Contact xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">
  <t:ItemClass>IPM.Contact</t:ItemClass>
  <t:Sensitivity>Normal</t:Sensitivity>
  <t:Importance>Normal</t:Importance>
  <t:Culture>en-US</t:Culture>
  <t:EmailAddresses>
    <t:Entry Key=""EmailAddress1"">support@rebex.net</t:Entry>
  </t:EmailAddresses>
</t:Contact>";

            EwsItemInfo contactInfo = EwsItemInfo.FromXml(sourceXml);
            EwsItemId contactId = ews.StoreItem(EwsFolderId.Contacts, contactInfo);
        Dim sourceXml As String =
"<t:Contact xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">
  <t:ItemClass>IPM.Contact</t:ItemClass>
  <t:Sensitivity>Normal</t:Sensitivity>
  <t:Importance>Normal</t:Importance>
  <t:Culture>en-US</t:Culture>
  <t:EmailAddresses>
    <t:Entry Key=""EmailAddress1"">support@rebex.net</t:Entry>
  </t:EmailAddresses>
</t:Contact>"

        Dim contactInfo As EwsItemInfo = EwsItemInfo.FromXml(sourceXml)
        Dim contactId As EwsItemId = ews.StoreItem(EwsFolderId.Contacts, contactInfo)

More item operations #

Additional item operations are described in detail on Message operations page.

Back to feature list...