locked
List Outlook tasks from the exchange server RRS feed

  • Question

  • User1482349280 posted

    I'm trying to write a server side page to connect to exchange and retrieve a list of the users tasks from Outlook. I've managed it locally with the Interop libraries but can't see how you can use this server side as this will need to talk to the exchange server not the outlook client application.

    Can anybody point me in the right direction please, any help much appreciated,

    Cheers

    Tuesday, December 12, 2006 6:28 AM

All replies

  • User-820230059 posted

    If you want to do it using exchange, you need to incorporate WebDav and a shallow traversal of the exchange HTTP data (web access)

    Here's an example of a webservice I wrote to get calendar information, you should be able to modify this to get tasks instead.

    Imports System.Web
    Imports System.Xml
    Imports System.Net
    Imports System.IO
    Imports System.Data
    Imports System.Security
    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    
    <WebService(Namespace:="http://*/")> _
    <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Public Class Service
        Inherits System.Web.Services.WebService
        Const errorLevel As Integer = 2  ' 2 = verbose, 1 = concise
        <WebMethod(Description:="This method returns the calendar entrys. It gets it's configuration from the web.config located in the application directory. It accepts a comma seperated string of names of calendars to open (IE: largeconferenceroom,smallconferenceroom,cvan,troom) It also accepts a single date, and returns all apts in that month for the date passed.")> _
        Public Function GetCalEntrys(ByVal Name As String, ByVal dDate As DateTime) As XmlDocument
            Dim strUsers As String()
            strUsers = Name.Split(",")
            Dim strUser As String = ""
            Dim ResponsexmlDoc As New XmlDocument
    
    
            Dim sb As New StringBuilder
            sb.Append("<root_add>")
            For Each strUser In strUsers
                sb.Append(Replace(LookUpCalendar(strUser, dDate).InnerXml.ToString, "<?xml version=""1.0""?>", ""))
            Next
            sb.Append("</root_add>")
            ResponsexmlDoc.LoadXml(sb.ToString)
            Return ResponsexmlDoc
        End Function
    
    
        Private Function LookUpCalendar(ByVal Name As String, ByVal dDate As DateTime) As XmlDocument
    
            Dim strURL As String = "http://" & ConfigurationManager.AppSettings("ServerName") & "/exchange/" & Name & "/calendar/"
    
            'Dim strRequest As String = "<?xml version=""1.0""?>" & _
            '          "<g:searchrequest xmlns:g=""DAV:"">" & _
            '          "<g:sql>SELECT ""urn:schemas:calendar:location"", ""urn:schemas:httpmail:subject"", " & _
            '          """urn:schemas:calendar:dtstart"", ""urn:schemas:calendar:dtend"", " & _
            '          """urn:schemas:calendar:busystatus"", ""urn:schemas:calendar:instancetype"" " & _
            '          "FROM Scope('SHALLOW TRAVERSAL OF """ & strURL & """') " & _
            '          "WHERE NOT ""urn:schemas:calendar:instancetype"" = 1 " & _
            '          "AND ""DAV:contentclass"" = 'urn:content-classes:appointment' " & _
            '          "AND ""urn:schemas:calendar:dtstart"" > '" & _
            '          String.Format("{0:yyyy/MM/dd}", dDate) & " 00:00:00' " & _
            '          "AND ""urn:schemas:calendar:dtend"" &lt; '" & _
            '          String.Format("{0:yyyy/MM/dd}", dDate.AddDays(32)) & " 00:00:00' " & _
            '          "ORDER BY ""urn:schemas:calendar:dtstart"" ASC" & _
            '          "</g:sql></g:searchrequest>"
    
            Dim strRequest As String = "<?xml version=""1.0""?>" & _
                      "<g:searchrequest xmlns:g=""DAV:"">" & _
                      "<g:sql>SELECT * " & _
                      "FROM Scope('SHALLOW TRAVERSAL OF """ & strURL & """') " & _
                      "WHERE NOT ""urn:schemas:calendar:instancetype"" = 1 " & _
                      "AND ""DAV:contentclass"" = 'urn:content-classes:appointment' " & _
                      "AND ""urn:schemas:calendar:dtstart"" > '" & _
                      String.Format("{0:yyyy/MM/dd}", dDate) & " 00:00:00' " & _
                      "AND ""urn:schemas:calendar:dtend"" &lt; '" & _
                      String.Format("{0:yyyy/MM/dd}", dDate.AddDays(32)) & " 00:00:00' " & _
                      "ORDER BY ""urn:schemas:calendar:dtstart"" ASC" & _
                      "</g:sql></g:searchrequest>"
    
            Dim strStatusText As String = ""
            Dim Status As Integer
            Dim ResponseXmlDoc As XmlDocument = SendRequest("SEARCH", strURL, strRequest, strStatusText, Status)
            Return ResponseXmlDoc
    
        End Function
    
        Function SendRequest(ByVal strCommand As String, ByVal strURL As String, ByVal strBody As String, _
            ByRef strStatusText As String, ByRef iStatCode As Integer) As XmlDocument
    
    
            ' Create a new CredentialCache object and fill it with the network credentials required to access the server.
            Dim Username As String = ConfigurationManager.AppSettings("UserForReadingCalendars")
            Dim Password As String = ConfigurationManager.AppSettings("PwdForReadingCalendars")
            Dim strDomain As String = ConfigurationManager.AppSettings("WindowsDomain")
    
            Dim myCred As New NetworkCredential(Username, Password, strDomain)
            Dim myUri As System.Uri = New System.Uri(strURL)
            Dim MyCredentialCache As New CredentialCache
            MyCredentialCache.Add(myUri, "NTLM", myCred)
    
            ' Create the HttpWebRequest object.
            Dim objRequest As HttpWebRequest = CType(WebRequest.Create(strURL), HttpWebRequest)
    
            ' Add the network credentials to the request.
            objRequest.Credentials = MyCredentialCache
    
            ' Specify the method.
            objRequest.Method = strCommand
    
            ' Set Headers
            objRequest.KeepAlive = True
            objRequest.Headers.Set("Pragma", "no-cache")
            objRequest.ContentType = "text/xml"
    
            'Set the request timeout to 5 minutes
            objRequest.Timeout = 300000
    
            If (strBody.Length > 0) Then
                ' Store the data in a byte array
                Dim ByteQuery() As Byte = System.Text.Encoding.ASCII.GetBytes(strBody)
                objRequest.ContentLength = ByteQuery.Length
                Dim QueryStream As Stream = objRequest.GetRequestStream()
                ' Write the data to be posted to the Request Stream
                QueryStream.Write(ByteQuery, 0, ByteQuery.Length)
                QueryStream.Close()
            End If
    
            ' Send the method request and get the response from the server.
            Dim objResponse As HttpWebResponse = CType(objRequest.GetResponse(), HttpWebResponse)
    
            ' Get the Status code
            iStatCode = objResponse.StatusCode
            strStatusText = objResponse.StatusDescription
    
            ' Get the XML response stream.
            Dim ResponseStream As System.IO.Stream = objResponse.GetResponseStream()
    
            ' Create the XmlDocument object from the XML response stream.
            Dim ResponseXmlDoc As New System.Xml.XmlDocument
            ResponseXmlDoc.Load(ResponseStream)
    
            ' Clean up.
            ResponseStream.Close()
            objResponse.Close()
            Return ResponseXmlDoc
    
        End Function
    
    End Class
     
    Tuesday, December 12, 2006 9:52 AM
  • User1482349280 posted

    Now that is what I call a reply, thanks for all that code, it's excellent.

    I thought I'd get your webservice working (as is) first to check that all the required environment settings are in place to allow me to do this, but when I run the code I get the following error:

    System.Net.WebException: The remote server returned an error: (403) Forbidden.

    at System.Net.HttpWebRequest.GetResponse()

    at WebService.SendRequest(String strCommand, String strURL, String strBody, String& strStatusText, Int32& iStatCode) in C:\Inetpub\wwwroot\TestVBStuff\App_Code\WebService.vb:line 124

     

    I've added the these keys to the web.config

    <add key="ServerName" value="my exchange server"/>

    <add key="UserForReadingCalendars" value=" my user name"/>

    <add key="PwdForReadingCalendars" value=" my password"/>

    <add key="WindowsDomain" value=" my domain"/>

    and then called the webservice using o.GetCalEntrys("rushton, graham", Now)

    Are there any config settings on the exchange server that need to be in place or does the user account need any special permissions? Added to this, am I calling it correctly? If I check within my outlook settings to get my exchange server name, the account is rushton, graham if that helps. Our outlook accounts are linked to our AD logon and I've also tried putting my network logon in place of the name but get the same error.

     Thanks again for all the code in your reply,

    Cheers

    Tuesday, December 12, 2006 10:36 AM
  • User-820230059 posted

    Make sure that you go to your webserver and turn Web Dav on.  It's usually OFF by default.

    I went over my code, and I think there may be a slight problem in the XML generation as well, but that you could work out easily enough.  It's just a root node thing.

    Tuesday, December 12, 2006 10:57 AM
  • User1482349280 posted

    Thanks, I think it may be the case that the Web Dav on our exchange servers is indeed OFF and I'm not sure our server support team are that keen to turn it on :-(   

    I'll try to convince them that it doesn't pose any security threats to the environment but this may take some time.

    Thanks for the info. I'll try and set up dev exchange box in the meantime and test the code that way but sometimes it takes longer to sort out the politics than it does to write the code.

    Many thanks. Cheers

    Tuesday, December 12, 2006 11:38 AM
  • User-378245712 posted

    Does web files has to be on the domain for it to work? Or Can I run the web services outside of the domain?

    Friday, January 4, 2008 5:39 PM
  • User-820230059 posted

    The webservice must be on a machine that has access to the active directory server.  It authenticates with exchanging using LDAP, using a domain username and password.

    Thursday, January 10, 2008 8:54 AM
  • User-378245712 posted

    I'm getting an error:

    System.Net.WebException: The remote server returned an error: (440) Login Timeout.
       at System.Net.HttpWebRequest.GetResponse()
       at AccessCalendar.SendRequest(String strCommand, String strURL, String strBody, String& strStatusText, Int32& iStatCode) in D:\Inetpub\wwwroot\OutLook\App_Code\AccessCalendar.vb:line 120
       at AccessCalendar.LookUpCalendar(String Name, DateTime dDate) in D:\Inetpub\wwwroot\OutLook\App_Code\AccessCalendar.vb:line 73
       at AccessCalendar.GetCalEntrys(String Name, DateTime dDate) in

     What would be causing the problem?

    Thanks...

    Tuesday, January 15, 2008 11:17 AM
  • User226384874 posted

    Hi Freak,

           Am need how to access Exchange server's calendar, and get the values of Appointment/Meeting items.

           I tried your code but I got an Error  "System.Net.WebException: The remote server returned an error: (401) Unauthorized"

         I need some sample code for how to accessing Exchange server calendar through my web applcation.

     

     

    Wednesday, April 9, 2008 1:27 AM
  • User-820230059 posted

    The above code should work for you if you're authorized to pull the calendar for the user you're trying to snag. 

    Make sure the web application server, is on the same domain as the exchange server, and that you are passing a valid username and password via the code I've posted above.

    Thursday, April 10, 2008 8:52 AM
  • User1976629052 posted

    Hi Bryan,

    WebDAV doesn't support retrieving task data  from Exchange store. I did a small POC on the same and saw that WebDAV is indeed not supported for tasks and journal information.

    Could you please let me know other ways of retriving the tasks from Exchange store? I know that we can use Exchange Web Services, but that's supported only for Exchange Server 2007.

    Friday, July 4, 2008 6:40 AM
  • User-820230059 posted

    Yea we ran into the same thing unfortunatly.  The solution is kind of complicated, but possibly I can point you the right direction. 

     If your exchange server is currently supporting mobile clients, such as smartphones and PDA's theres a device specific set of pages that it will expose for interaction with the devices.  You can interact with these pages the same way by impersonating the mobile device.  This will give you access to contacts, tasks, email, and calendars - though it's not as clean to look at the calendar this way, as it is the way I presented above.

     

    Friday, July 4, 2008 7:20 AM
  • User1976629052 posted

    I too agree that its not the cleaner way to acheive what we want. I guess we have the only option of EWS

    Friday, July 4, 2008 7:30 AM
  • User-1526740854 posted

     

    Hi All, Anyone can tell how can can I convert the output XmlDocument to a dataset coming from the exchange server. As it contain namespaces i can not able to do it.

     I am using the webservice written by Freak in the above which will return xmldocument.

    Please help me.

     Thanks in advance

    Friday, August 28, 2009 9:14 AM