程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 在ASP.NET 使用WebDAV訪問Outlook的日歷

在ASP.NET 使用WebDAV訪問Outlook的日歷

編輯:.NET實例教程

 在很多公司裡,會議室的預定是通過Outlook的Public文件夾實現的,但是如果你需要會議室的同時,也需要投影儀和筆記本,那麼你至少需要預定三處,Dotnetcms提供的會議室預定系統則很好的解決這個問題,演示地址為 http://demo.dotnetcms.org/dotnetcms/book/VIEwcalendar.ASPx (我們的主頁地址為  http://www.dotnetcms.org )

   在會議室預定裡,實現了和Outlook的同步,這裡使用了WebDAV協議,使用它可以對Outlook的日歷進行讀寫。  

   當訪問Outlook的calendar的Uri時,首先需要建立一個Table來存放返回的數據,為此定義了一個CreateDataTable方法,如下

 

    #region 創建一個臨時表來存放日歷Calendar信息

 

    public DataTable CreateDataTable()
    {

        System.Data.DataTable table = new DataTable("calendar");
        DataColumn column;


        // Create new DataColumn, set DataType, 
        // ColumnName and add to DataTable.    
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.Int32");
        column.ColumnName = "id";
        column.AutoIncrement = true;
        column.ReadOnly = true;
        column.Unique = true;
        table.Columns.Add(column);

        // Create second column.
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "subject";
        column.AutoIncrement = false;
        column.Caption = "subject";
        column.ReadOnly = false;
        column.Unique = false;
        // Add the column to the table.
        table.Columns.Add(column);

 

 


        // Create 3 column.
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "location";
        column.AutoIncrement = false;
        column.Caption = "location";
        column.ReadOnly = false;
        column.Unique = false;
   &nbs
p;    // Add the column to the table.
        table.Columns.Add(column);


        // Create 4 column.
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "dtstart";
        column.AutoIncrement = false;
        column.Caption = "dtstart";
        column.ReadOnly = false;
        column.Unique = false;
        // Add the column to the table.
        table.Columns.Add(column);


        // Create second column.
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "dtend";
        column.AutoIncrement = false;
        column.Caption = "dtend";
        column.ReadOnly = false;
        column.Unique = false;
        // Add the column to the table.
        table.Columns.Add(column);


        // Create second column.
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "busystatus";
        column.AutoIncrement = false;
        column.Caption = "busystatus";
        column.ReadOnly = false;
        column.Unique = false;
        // Add the column to the table.
        table.Columns.Add(column);


        // Create second column.
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "instancetype";
        column.AutoIncrement = false;
        column.Caption = "instancetype";
        column.ReadOnly = fa
lse;
        column.Unique = false;
        // Add the column to the table.
        table.Columns.Add(column);

 

 

 


        // Make the ID column the primary key column.
        DataColumn[] PrimaryKeyColumns = new DataColumn[1];
        PrimaryKeyColumns[0] = table.Columns["id"];
        table.PrimaryKey = PrimaryKeyColumns;

        return table;

    }


    #endregion

 

 

接下來,就需要從Exchange 服務器上抓取數據,在這裡WebDAV除了支持Http輕求常用的GET、POST、HEAD等方法外,還額外提供了幾個謂詞,下面就是使用“Search”方法來從服務器上獲取數據

     strQuery = "<?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 \"" + strCalendarURI + "\"') "
            + "WHERE NOT \"urn:schemas:calendar:instancetype\" = 1 "

           + " and \"DAV:contentclass\" = 'urn:content-classes:appointment' "
            + "AND \"urn:schemas:calendar:dtend\" > '" + dt + "' "
            + "ORDER BY \"urn:schemas:calendar:dtstart\" ASC"
                 + "</g:sql></g:searchrequest>";

      

如果你仔細看,你會發現他的查詢預防和SQL非常類似, select  url:schemas:calendar:location ... from scope() where not

這和數據庫的SQL幾乎一樣

 

 

        MyCredentialCache = new System.Net.CredentialCache();
        MyCredentialCache.Add(new System.Uri(strCalendarURI), "NTLM", new System.Net.NetworkCredential(System.Web.Configuration.WebConfiguratio
nManager.AppSettings["username"].ToString(), System.Web.Configuration.WebConfigurationManager.APPSettings["passWord"].ToString(), System.Web.Configuration.WebConfigurationManager.APPSettings["domain"].ToString()));

        Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strCalendarURI);
        Request.Credentials = MyCredentialCache;
        Request.Method = "SEARCH";
        bytes = Encoding.UTF8.GetBytes((string)strQuery);
        Request.ContentLength = bytes.Length;
        RequestStream = Request.GetRequestStream();
        RequestStream.Write(bytes, 0, bytes.Length);
        RequestStream.Close();

        Request.KeepAlive=true;
        Request.Headers.Set("Pragma", "no-cache");
        Request.ContentType = "text/XML";
        Request.Timeout = 300000;
        Response = (HttpWebResponse)Request.GetResponse();
        ResponseStream = Response.GetResponseStream();
        ResponseXmlDoc = new XMLDocument();
        ResponseXMLDoc.Load(ResponseStream);

 

獲取數據後,就需要把數據存放到剛才我們建立的臨時表裡,

        SubjectNodeList = ResponseXMLDoc.GetElementsByTagName("e:subject");
        LocationNodeList = ResponseXMLDoc.GetElementsByTagName("d:location");
        StartTimeNodeList = ResponseXMLDoc.GetElementsByTagName("d:dtstart");
        EndTimeNodeList = ResponseXMLDoc.GetElementsByTagName("d:dtend");
        BusyStatusNodeList = ResponseXMLDoc.GetElementsByTagName("d:busystatus");
        InstanceTypeNodeList = ResponseXMLDoc.GetElementsByTagName("d:instancetype");

        DataTable table = CreateDataTable();


        if (SubjectNodeList.Count > 0)
        {
            for (int i = 0; i < SubjectNodeList.Count; i++)
            {
                DataRow row;
                row = table.NewRow();

                row["subject"] = SubjectNodeList[i].InnerText;
                row["location"] = LocationNodeList[i].InnerText;
                row["dtstart"] = StartTimeNodeList[i].InnerText;
                row["dtend"] = EndTimeNodeList[i].InnerText;
                row["busystatus"] = BusyStatusNodeList[i].InnerText;
                row["instancetype"] = InstanceTypeNodeList[i].InnerText;
                table.Rows.Add(row);

            }
        }


        ResponseStream.Close();
        Response.Close();

        return table;
    }

 

這樣,數據就獲取成功了,當你一旦獲取成功後,就可以進行各種操作了。

 

接下來,我們就可以把數據綁定到GrdivIEw1上進行顯示或者各種樣式處理,當然更推薦你使用DayPiolet控件,這樣就可以讓自定義的日歷與Outlook同步。

 

 

綁定DayPiolet預覽效果的地址為 http://demo.dotnetcms.org/dotnetcms/book/VIEwcalendar.ASPx (注意:這裡的演示是使用MSSQL 數據庫為基礎,因為空間根本沒有Exchange 服務器,這裡只是模擬)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved