Tag Archives: XMLHTTP

Exchange Server: How to retrieve appointments using C# & WebDAV?


Code Snippet (C#):

   //Declaration part
    string strExchSvrName = “”;
    string strMailbox = “”;
    string strCalendarUri = “”;
    string strDomain = “”;
    string strUserName = “”;
    string strPassword = “”;
    System.Net.HttpWebRequest WebDavRequest = null;
    System.Net.HttpWebResponse WebDavResponse = null;
    System.Net.CredentialCache MyCredentialCache = null;
    byte[] bytes = null;
    System.IO.Stream WebDavRequestStream = null;
 
     // Provide the Exchange server name;
      strExchSvrName = “mydomain.in”;
 
      // Provide Mailbox folder name.
       strMailbox = “Mailbox1”;
 
      //if HTTPS -then provide URI of the user’s calendar folder
      strCalendarUri = “https://” + strExchSvrName + “/exchange/”+ strMailbox + “/Calendar/”;
 
        //if HTTP -then provide URI of the user’s calendar folder
        //strCalendarUri = “http://” + strExchSvrName + “/exchange/” + strMailbox + “/Calendar/”;
 
        //Provide the User name and password of appointment creator. Make sure the user has enough permissions to read
        strUserName = “username”; 
        strDomain = “domain”;
        strPassword = “password”;
 
        //Build the Query
        string query = “<?xml version=”1.0”?><D:searchrequest xmlns:D = “DAV:” >”
                + “<D:sql>SELECT “http://schemas.microsoft.com/exchange/outlookmessageclass”, “DAV:contentclass”, 
“DAV:displayname” FROM “”
+ strCalendarUri + “””
                + “WHERE “DAV:ishidden” = true AND “DAV:isfolder” = false”
                + “</D:sql></D:searchrequest>”;
 
        query = “<?xml version=”1.0″?>”
                + “<dav:searchrequest xmlns:dav=”DAV:”>”
                + “<dav:sql>”
                + “SELECT “
                + “”DAV:displayname”, “ // Appointment Uri portion of the resource
                + “”DAV:href”, “ // Full resource Uri of the appointment
                + “”urn:schemas:httpmail:subject”, “ // Subject of appointment
                + “”urn:schemas:calendar:dtstart”, “ // Start date/time of appointment
                + “”urn:schemas:calendar:dtend”, “ // End date/time of appointment
                + “”urn:schemas:httpmail:textdescription”, “ // Body of appointment
                + “”urn:schemas:calendar:location”, “ // Location of appointment
                + “”urn:schemas:calendar:alldayevent”, “ // Whether appointments an all day appointment
                + “”urn:schemas:calendar:meetingstatus”, “ // Confimed status of the appointment
                + “”urn:schemas:calendar:busystatus”, “ // Comitted status the appointment represents
                + “”http://schemas.microsoft.com/mapi/proptag/x823D0003”, “ // Color of the appointment
                + “”urn:schemas:calendar:reminderoffset”, “ // Reminder offset of the appointment
                + “”DAV:ishidden”, “ // Whether this is hidden
                + “”urn:schemas:calendar:instancetype”, “ // Relation of the appointment to a recurring series
                + “”urn:schemas:calendar:transparent”, “ // Transparency of the appointment to free/busy searches
                + “”urn:schemas:calendar:timezoneid” “ // Display timezone of the appointment
                + “FROM Scope(‘SHALLOW TRAVERSAL OF “” + strCalendarUri + “”‘) “
                + “WHERE “
                + “(“DAV:contentclass” = ‘urn:content-classes:appointment’ ) “ // Item is an appointment
                + “AND (NOT “urn:schemas:calendar:instancetype” = 1) “ // appointment is not the master of a recurring series (Get single
appointments or instances of recurring appointments)
                + “AND (“urn:schemas:calendar:dtend” &gt; ‘” + 
                Convert.ToDateTime(“3-12-2007”).Date.ToUniversalTime().ToString(“yyyy/MM/dd HH:mm:ss”) + “‘)
//Appointment ends after the start of our range
                + “AND (“urn:schemas:calendar:dtstart” &lt; ‘” + 
Convert.ToDateTime(“3-17-2007”).Date.ToUniversalTime().ToString(“yyyy/MM/dd HH:mm:ss”) + “‘)
//Appointment begins before the end of our range
                + “AND (“DAV:displayname” like ‘” + “LS.ImportantDates.” + “%’ ) “
                + “ORDER BY “urn:schemas:calendar:dtstart” “
                + “</dav:sql>”
                + “</dav:searchrequest>”;
 
 
        //Provide credentials required to access the server. Here i used the NTLM
        MyCredentialCache = new System.Net.CredentialCache();
        MyCredentialCache.Add(new System.Uri(strCalendarUri), “NTLM”, new System.Net.NetworkCredential(strUserName, strPassword, strDomain));
 
        //Create the HttpWebRequest object.
        WebDavRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(strCalendarUri);
 
        // Add the network credentials to the request.
        WebDavRequest.Credentials = MyCredentialCache;
 
        // Specify the PROPPATCH method.
        WebDavRequest.Method = “SEARCH”;
 
        // Encode the body using UTF-8.
        bytes = Encoding.UTF8.GetBytes(query);
 
        // Set the content header length.
        WebDavRequest.ContentLength = bytes.Length;
 
        // Get a reference to the request stream.
        WebDavRequestStream = WebDavRequest.GetRequestStream();
 
        // Write the message body to the request stream.
        WebDavRequestStream.Write(bytes, 0, bytes.Length);
 
        // Release the connection
        WebDavRequestStream.Close();
 
        // Set the content type header.
        WebDavRequest.ContentType = “text/xml”;
 
       WebDavResponse = (System.Net.HttpWebResponse)WebDavRequest.GetResponse();
       Response.Write(“Calendar : “);
  Response.Write(“Status Code: “ + WebDavResponse.StatusCode + “Description: “ + WebDavResponse.StatusDescription);
       using (System.IO.StreamReader streamReader = new System.IO.StreamReader(WebDavResponse.GetResponseStream()))
       {
             Response.Write(streamReader.ReadToEnd());
       }
       WebDavResponse.Close();
        
 
     You can get more related samples and detailed information from Dan’s blog. For more information you can also refer this article .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

How to authenticate the Inbox in Microsoft Exchange Server 2003 with forms-based authentication enabled?


Please find the Code Snippet Access Exchange Server 2003 using WebDAV in your Web application per KB:



   1:  ‘Declaration Section
   2:  Dim strServerName as String = “Server Name”     ‘TODO: Change to your environment
   3:  Dim strDomain as String = “Domain Name”       ‘TODO: Change to your environment
   4:  Dim strUserID as String = “Username”            ‘TODO: Change to your environment
   5:  Dim strPassword as String = “Password”        ‘TODO: Change to your environment
   6:   
   7:  ‘ Create our destination URL.
   8:  Dim strURL As String = “https:/” & strServerName & “/exchange” & strUserName & “/inbox/test.eml”
   9:  Dim strReusableCookies As String
  10:   
  11:  ‘ Create our Web request object.
  12:  GETRequest = CType(WebRequest.Create(New System.Uri(strURL & strApptItem)), HttpWebRequest)
  13:  strReusableCookies = AuthenticateSecureOWA(strServerName, strDomain, strUserID, strPassword)
  14:   
  15:  ‘ Add the cookie set that is obtained after OWA authentication to our request header.
  16:  PROPPATCHRequest.Headers.Add(“Cookie”, strReusableCookies)
  17:  PROPPATCHRequest.ContentType = “text/xml”
  18:  PROPPATCHRequest.KeepAlive = True
  19:  PROPPATCHRequest.AllowAutoRedirect = False
  20:   
  21:  ‘ Specify the PROPPATCH method.
  22:  PROPPATCHRequest.Method = “GET”
  23:   
  24:  ‘ Enter your WebDAV-related code here.
  25:  

.csharpcode {
BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, “Courier New”, courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, “Courier New”, courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
MARGIN: 0em
}
.csharpcode .rem {
COLOR: #008000
}
.csharpcode .kwrd {
COLOR: #0000ff
}
.csharpcode .str {
COLOR: #006080
}
.csharpcode .op {
COLOR: #0000c0
}
.csharpcode .preproc {
COLOR: #cc6633
}
.csharpcode .asp {
BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
COLOR: #800000
}
.csharpcode .attr {
COLOR: #ff0000
}
.csharpcode .alt {
BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
COLOR: #606060
}

There are two ways of implementing this.


Method # 1: In the Visual Basic .NET code sample that was just mentioned, the strReusableCookies string variable is the authentication cookie that is returned from the AuthenticateSecureOWA function call. If the authentication cookie times out, call the AuthenticateSecureOWA function again to receive a new authentication cookie.

Method # 2: Put the WebDAV request in a try/catch block. The try/catch block will catch the authentication cookie time-out error. When the authentication cookie time-out error occurs, you can re-authenticate the Inbox in Exchange Server 2003 to the Exchange Server 2003 server that is enabled with forms-based authentication.

Web Service : Passing data to and from Webservice & SOAP header


SOAP headers offer a method for passing data to and from an XML Web service method if the data is not directly related to the XML Web service method’s primary functionality.

For instance, an XML Web service might contain several XML Web service methods that each require a custom authentication scheme. Instead of adding parameters to each XML Web service method for the custom authentication scheme, a SoapHeaderAttribute, referring to a class deriving from SoapHeader, can be applied to each XML Web service method. The implementation for the class deriving from SoapHeader handles the custom authentication scheme.

In this manner, the XML Web service method implements only the functionality specific to it and adds additional functionality using a SOAP header.

Reference: http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapheader.aspx

Exchange Web Service: Web Services Message cycle


Exchange Web Services Message cycle 



  • When a client application requests information from the Exchange store, an XML request message that complies with the SOAP standard is created and sent to the Exchange server.

  • When the Microsoft Exchange server receives the request, it verifies the credentials that are provided by the client and automatically parses the XML for the requested data.

  • The server then builds a SOAP response that contains XML data that represents the requested strongly typed objects and their properties.

  • The XML data is sent back to the client application in an HTTP response.

  • The client application then deserializes the XML and uses the data to reform the strongly typed objects.

 

WebDAV Sample(s): How to get item property values from Exchange Store


Please find this wonderful article, which talks about “How to get item property values using WebDAV“. This article has good example’s in VB.Net, C#.Net, C++.Net and VBScript. 


It talks about, how you can construct the XML body of a WebDAV PROPFIND Method manually. The request is for the displayname Field for a folder. After the request has been constructed, the code passes the XML string to an XMLHTTP Component Object Model (COM) object and sends the PROPFIND Method request to the server.

Monitoring Event sink # 7 – Register an Exchange Store Event Script File Using WebDAV in Visual Basic 6.0


I found this wonderful link, which discuss about how we can register an Exchange Store Event Sink using WebDAV in Visual Basic 6.0


Please find the WebDavRegEvent sample shows how to register a Store Event Sink using Web Distributed Authoring and Versioning (WebDAV) in Microsoft Visual Basic 6.0.



  • The code uses XML technology to compose and send the WebDAV request to the Exchange Server.

  • The sample does not contain an executable and will require Microsoft Visual Basic 6.0 and Microsoft XML 2.0 or later on the computer in order to view and build the project.

  • The sample shows how to register a simple event sink.

  • It does not explore all areas of creating a registration item for an Exchange event sink.