国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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

2019-11-17 03:48:37
字體:
來源:轉載
供稿:網友

      在很多公司里,會議室的預定是通過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;
        // 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 = false;
        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.WebConfigurationManager.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 服務器,這里只是模擬)


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄄城县| 石渠县| 恩施市| 阿鲁科尔沁旗| 景洪市| 大兴区| 京山县| 金乡县| 兴宁市| 叙永县| 同仁县| 晋州市| 宁晋县| 晋州市| 大悟县| 泰来县| 大埔区| 大足县| 蚌埠市| 嘉义县| 绥芬河市| 咸阳市| 珲春市| 忻城县| 应用必备| 丰原市| 阳原县| 高尔夫| 汉源县| 南宁市| 宜城市| 耒阳市| 即墨市| 灵宝市| 湖州市| 栾城县| 西藏| 博爱县| 武功县| 大厂| 兰西县|