sharepoint列表EventHandle的開發

 

發佈infopath模板到sharepoint站點這一篇中,我們看到了Infopath的web效果,是很挺酷的,但在我們的一些日常應用中,用表單收集來的部分數據,最好呢是在創建表單的同時,能有一份數據同時也能進入到相應數據庫裏面,以便日後的數據分析。這想法看起來挺不錯的,但具體要怎樣實現呢?剛好,在sharepoint中有事件這個概念,在開發者中常被稱爲EventHandle。無論是對網站的操作還是對文檔庫、列表庫、列表項的操作,但可以觸發相關的Event。正基於此,我們可以通過Event的方式,當創建一個文檔時,將文檔中的相關數據存放到SQL數據庫中。至此,分析完畢,具體操作,請看下面實現步驟:
第一步:
先在SQL數據庫裏創建表,表名爲“報銷單”,具體字段如下圖:

用來存放表單中的數據。
第二步:
用VS一個項目,創建類型爲類庫,項目名稱爲“EventHandle”。並將Class1.cs改名爲listEventHandle.cs。在引用中加入Microsoft.SharePoint.dll。以下爲listEventHandle.cs的原碼,定義了列表的增,刪,改事件:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Data.SqlClient;

namespace EventHandle
{
    public class listEventHandle:SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            SynchronizationSQL(properties);
        }
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            SynchronizationSQL(properties);
        }

        public SqlConnection createConn()
        {
            SqlConnection conn = new SqlConnection("Data Source=cdh10000;Initial Catalog=oalistDataToTable;User ID=sa;Password=111111");
            conn.Open();
            return conn;

        }
       
        public override void ItemDeleting(SPItemEventProperties properties)
        {
            SPListItem item = properties.ListItem;
            string itemid = properties.ListItem.ID.ToString();

            SqlConnection conn = createConn();
           
            SqlCommand cmd = new SqlCommand("delete from 報銷單 where listitem_id='" + itemid + "'", conn);
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        void SynchronizationSQL(SPItemEventProperties properties)
        {
            SPListItem item = properties.ListItem;
            string itemid = properties.ListItem.ID.ToString();
            string name = item["姓名"].ToString();
            string department = item["部門"].ToString();
            string timeto = item["時間"].ToString();
            string money = item["金額"].ToString();
            string eventto = item["事由"].ToString();

            SqlConnection conn = createConn();

            SqlCommand cmd = new SqlCommand("select listitem_id from 報銷單 where listitem_id='" + itemid + "'", conn);

            if (Convert.ToInt32(cmd.ExecuteScalar()) > 0)
            {
                cmd = new SqlCommand("update 報銷單 set 姓名='" + name + "',部門='" + department + "',時間='" + timeto + "',金額='" + money + "',事由='" + eventto + "' where listitem_id='" + itemid + "'", conn);
                cmd.ExecuteNonQuery();
            }
            else
            {
                cmd = new SqlCommand("insert into 報銷單(listitem_id,姓名,部門,時間,金額,事由) values('" + itemid + "','" + name + "','" + department + "','" + timeto + "','" + money + "','" + eventto + "')", conn);
                cmd.ExecuteNonQuery();
            }
          
            conn.Close();

        }
     

    }
}

最後別忘了加入強名稱,生成項目。用Reflector.exe獲取EventHandle.dll的程序集名稱跟公鑰.並將EventHandle.dll拖入
c:/WINDOWS/assembly文件夾中。重啓IIS。

第三步:
用VS再創建一個控制檯應用程序項目:項目名稱爲“EventRunning”。在引用中加入Microsoft.SharePoint.dll。在Program.cs中加入代碼如下:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Data.SqlClient;

namespace EventRunning
{
    class Program
    {
        static void Main(string[] args)
        {
            SPSite site = new SPSite("http://cdh10000");
            SPWeb web = site.OpenWeb();
            SPList list = web.Lists["報銷單"];


            string asmName = "EventHandle, Version=1.0.0.0, Culture=neutral, PublicKeyToken=01dc07d3d1e20903";//程序集名稱
            string className = "EventHandle.listEventHandle";//程序集中的類名

            list.EventReceivers.Add(SPEventReceiverType.ItemAdded, asmName, className);
            list.EventReceivers.Add(SPEventReceiverType.ItemUpdated, asmName, className);
            list.EventReceivers.Add(SPEventReceiverType.ItemDeleting, asmName, className);
           
        }
    }
}

運行項目即可。至此,報銷單文檔庫的Event事件已經完成。
第四步:
就是查看我們的成果了,let's go!
進入報銷單庫,新建一個表單,填寫表單內容如下,


再看一下SQL數據庫表中的數據,如下圖:


呵呵,SQL數據庫中的數據與表單數據剛好一致,酷吧!這時大家就可以發揮一下想像力了......

發佈了19 篇原創文章 · 獲贊 1 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章