logo

ASP.NET How to communicate with the Exchange Web Services EWS Soap layer .

You must install EWS as an "Add webservice". The installation will create a app_webreference directory with a exchangewebservices subdirectory and the following files : messages.xsd, services.wsdl, and types.xsd. The NetworkCredential uses your username, password, and exchange server domain to authenticate to microsofts NTLM protocols. (http://en.wikipedia.org/wiki/NTLM) . You define the exchangeservicebinding url as the location were the webservice code resides, this is part of exchange server. The calendarView is a EWS filter type. findItemRequest uses EWS to find items in specific folders. ItemResponseShapeType sets the properties of the finditemrequest. DistinguishedFolderIdType class identifies Microsoft Exchange Server folders that can be referenced by name. (http://msdn.microsoft.com/en-us/library/exchangewebservices.distinguishedfolderidtype(v=exchg.140).aspx) ResponseMessages.Items gets messages back from the service broker. Each message forms a conversation. Each message and conversation has a specific type. The ResponseMessageType class is the base class for all response messages. ArrayOfResponseMessagesType gives you access to EWS ResponseMessageType. You then can cycle through each responsemessagetypes in the array. FindItemParentType contains the results of a search of a single root folder. (http://msdn.microsoft.com/en-us/library/bb402192(v=exchg.140).aspx) Cycle through an array of items under the root node and look for calendaritemtype then load the user defined class into an arraylist to be display in a calendar render day event.
<%@ Import Namespace="ExchangeWebServices" %>
<%@ Import Namespace="EWSClientProxyAssembly" %>
    Class cCalendarInfo
        Public sLocation As String
        Public sStart As String
        Public sEnd As String
        Public sSubject As String
    End Class
  Function ProcessExchangeServerCalendarEvents(ByVal sCurrentDate As String) As ArrayList
        Dim esb As New ExchangeServiceBinding
        Dim findItemRequest As FindItemType = New FindItemType
        Dim calendarView As CalendarViewType = New CalendarViewType
        Dim CalendarItems As New ArrayList

	
        esb.Credentials = New NetworkCredential("your_username", "your_password", "mail.your_domain.com")
        esb.RequestServerVersionValue = New RequestServerVersion
        esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2010
        esb.Url = "https://mail.your_domain.com/EWS/Exchange.asmx"
        esb.UseDefaultCredentials = True
        
        'calendarView.StartDate = DateTime.Now.AddDays(-30)
        'calendarView.EndDate = DateTime.Now.AddDays(0)

        calendarView.StartDate = sCurrentDate
        calendarView.EndDate = sCurrentDate

        calendarView.MaxEntriesReturned = 100
        calendarView.MaxEntriesReturnedSpecified = True

        'Dim foldername As String()
        findItemRequest.Item = calendarView
        
        Dim itemProperties As ItemResponseShapeType = New ItemResponseShapeType()
        itemProperties.BaseShape = DefaultShapeNamesType.AllProperties
        
        findItemRequest.ItemShape = itemProperties
        Dim folderIDArray(2) As DistinguishedFolderIdType
        
        folderIDArray(0) = New DistinguishedFolderIdType()
        folderIDArray(0).Id = DistinguishedFolderIdNameType.calendar
        
        findItemRequest.ParentFolderIds = folderIDArray
                
        findItemRequest.Traversal = ItemQueryTraversalType.Shallow

        Try
            Dim findItemResponse As FindItemResponseType = esb.FindItem(findItemRequest)
	    'Array of Response Messages
            Dim responseMessages As ArrayOfResponseMessagesType = findItemResponse.ResponseMessages

            Dim rmta As ResponseMessageType() = responseMessages.Items
            Dim rmt As New ResponseMessageType
            ' Cycle through each responsetype in the array
            For Each rmt In rmta
                Dim firmt As FindItemResponseMessageType = rmt
                
		'Find the single root folder of the responsemessagetype
                Dim fipt As FindItemParentType = firmt.RootFolder
                Dim obj As Object = fipt.Item
                If TypeOf obj Is ArrayOfRealItemsType Then
	            'Type case to an array of items
                    Dim items = CType(obj, ArrayOfRealItemsType)
                    
                    Dim it As New ItemType
                    For Each it In items.Items
                        If TypeOf it Is CalendarItemType Then
                            Dim cal As New CalendarItemType
                            cal = CType(it, CalendarItemType)
                            Dim ce As New cCalendarInfo
                            With ce
                                .sLocation = cal.Location
                                .sStart = cal.Start
                                .sEnd = cal.End
                                .sSubject = cal.Subject
                            End With
                            CalendarItems.Add(ce)
                        End If
                            
                    Next
                End If
            Next
                
        Catch ex As Exception
            oFnc.EmailNotifyText("Failed to access EWS", "your_email", ex.Message)
        End Try
        
        'Dim sb As New StringBuilder
        'Dim i As Integer
        'Dim ce2 As cCalendarInfo

        'For i = 0 To CalendarItems.Count - 1
        '    ce2 = CalendarItems(i)
        '    sb.Append("<br>" & ce2.sLocation & "," & ce2.sStart & "," & ce2.sEnd & "," & ce2.sSubject)
        'Next

        'lblITCalendar.Text = sb.ToString
        Return (CalendarItems)
    End Function
    Sub IT_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs)
        Dim sDate As String
        Dim alCalendarItems As ArrayList
        Dim i As Integer
        Dim oCalendarInfo As cCalendarInfo
        Dim sBuffer As String
        Dim oLabel As Label

        
        sDate = e.Day.Date
        
        alCalendarItems = ProcessExchangeServerCalendarEvents(sDate)
        

        For i = 0 To alCalendarItems.Count - 1
            oCalendarInfo = alCalendarItems(i)
            sBuffer = ""
            With oCalendarInfo
                sBuffer = "<br>" & .sLocation & " " & .sSubject
            End With
            oLabel = New Label
            oLabel.Text = sBuffer
            e.Cell.Controls.Add(oLabel)
        Next

    End Sub
s