• How to use Microsoft Exchange WebServices (EWS) with vb.net

    If you use Microsoft Exchange 2007 SP1 or later you use Microsoft Exchange WebServices (aka EWS) to access users data, e.g. all appointments from Calendar. If the user is already logged in using Windows Credentials and these are passed in the browser, no additionall login is required. That’s convenient for the user, compared to e.g. IMAP where you always have to supply your windows password.
    So here are some details which might help you if you want to use Microsofts Exchange Webservices:


    1. Install Microsoft Exchange Web Services Managed API 2.0 on your developer client, available at https://www.microsoft.com/en-us/download/details.aspx?id=35371
    2. Add reference to Microsoft.Exchange.WebServices.dll file within your project as described at http://msdn.microsoft.com/en-us/library/dd633626%28v=exchg.80%29.aspx

    Get Access to the service:

    The following Function establishes the connection to the service. If the user is not connected to Exchange SP1 or later, nothing is returned.
        ”’ <summary>
    ”’ Email Address for which the Appointments should be retrieved
    ”’ </summary>
    ”’ <param name=”emailAddress”>Valid email address, e.g. Test@nullexample.com</param>
    ”’ <returns>Service, otherwise nothing</returns>
    ”’ <remarks></remarks>
    Friend Shared Function GetService(emailAddress As String) As Microsoft.Exchange.WebServices.Data.ExchangeService
    Dim Service As Microsoft.Exchange.WebServices.Data.ExchangeService = Nothing
    Service = New Microsoft.Exchange.WebServices.Data.ExchangeService(Microsoft.Exchange.WebServices.Data.ExchangeVersion.Exchange2007_SP1)
    ‘ Microsoft recommends to use AutoDiscoverUrl to ensure that always the best endpoint for the given user is used.
    Service.UseDefaultCredentials = True
    Catch ex As Microsoft.Exchange.WebServices.Data.AutodiscoverLocalException
    ‘Seems that user does not work on Exchange 2007 SP1 or later?
    End Try
    Return Service
    End Function

    Note: To get the email address of current user you might use System.DirectoryServices.AccountManagement.UserPrincipal.Current.EmailAddress.


    Create DataTable with all Appointments

    The following function creates a table with all existing appointments of current user within the defined timerange. In Appointments-Datatable I have the structure already defined. As you see I use some basic data from the appointments, e.g. Start, End, Subject etc. If you want more properties don’t forget to add these to the service.LoadPropertiesForItems, otherwise they are not accessible.

        Imports Microsoft.Exchange.WebServices.Data

    ”’ <summary>
    ”’ Retrieve a table with all appointments found in given mailbox in given folder
    ”’ </summary>
    ”’ <param name=”appointments”>Datatable containing all appointments from current user which are already retrieved before</param>
    ”’ <returns>Datatable with appointments if appointments are found in given folder. Otherwise nothing.</returns>
    ”’ <remarks></remarks>
    Private Overloads Function GetAllAppointmentsFromMailbox(service As ExchangeService, retrievalDays As Integer, appointments as DataTable) As DataTable
    Dim cFolder As Folder
    Dim cView As CalendarView
    Dim findResults As FindItemsResults(Of Appointment)
    Dim count As Integer
    Dim CutOffDate As DateTime
    Dim CutOffDateFuture As DateTime

    CutOffDate = Now.AddDays((-1) * retrievalDays)
    CutOffDateFuture = Now.AddDays(retrievalDays)

    cFolder = Folder.Bind(service, WellKnownFolderName.Calendar)
    cView = New CalendarView(CutOffDate, CutOffDateFuture)
    findResults = service.FindAppointments(cFolder.Id, cView)
    count = findResults.Count
    If count > 0 Then
    service.LoadPropertiesForItems(findResults.Items, New PropertySet(AppointmentSchema.Subject, AppointmentSchema.Location, AppointmentSchema.Body, _
    AppointmentSchema.Start, AppointmentSchema.End, AppointmentSchema.IsAllDayEvent, _
    AppointmentSchema.Body, AppointmentSchema.IsRecurring, AppointmentSchema.Recurrence, _
    AppointmentSchema.Id, AppointmentSchema.ICalUid, AppointmentSchema.OptionalAttendees, _
    AppointmentSchema.RequiredAttendees, AppointmentSchema.Organizer, AppointmentSchema.MyResponseType))

    Dim CategoryIDForNewAppointments As Integer = CInt(ConfigurationManager.AppSettings(“CategoryIDForNewAppointments”))
    Dim i As Integer
    Dim Row As DataRow
    Dim Participant As Attendee
    Dim Participants As String
    For i = 0 To findResults.TotalCount – 1
    With findResults.Items(i)
    If Appointments.Rows.Find(.ICalUid) Is Nothing Then
    Row = Appointments.NewRow
    Row(“UID”) = .ICalUid
    Row(“StartDate”) = .Start
    Row(“EndDate”) = .End
    Row(“Subject”) = .Subject
    Row(“Description”) = .Body
    Row(“Location”) = .Location
    Row(“Organizer”) = .Organizer.Name
    Participants = String.Empty
    For Each Participant In .RequiredAttendees
    If String.IsNullOrWhiteSpace(Participants) Then
    Participants = Participant.Name
    Participants &= “; ” & Participant.Name
    End If
    For Each Participant In .OptionalAttendees
    If String.IsNullOrWhiteSpace(Participants) Then
    Participants = Participant.Name
    Participants &= “; ” & Participant.Name
    End If
    Row(“Participants”) = Participants
    Row(“Status”) = .MyResponseType.ToString
    End If
    End With
    GetAllAppointmentsFromMailbox = Appointments
    GetAllAppointmentsFromMailbox = Nothing
    End If

        End Function


    Using EWS on a Webserver

    If you want to use EWS within an ASP.net application on your IIS you need to activate “Trust computer for delegation”. As the description says: “This security-sensitive option means that services running as localsystem on this computer can request services from other servers.” If it’s deactivated you won’t be able to access any mailboxes on your exchange servers using EWS. So go to your active directory, open the computer account of your IIS application server and tick the checkbox. See also MSDN Blog entry EWS from a Web Application using Windows Authentication and Impersonation.


Leave a comment

If you want to share your opinion, leave a comment.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">