sharepoint2010獲取文檔點擊數

我的做法是通過HttpModule來實現,具體做法:

1.做一個類庫,實現IHttpModule藉口;

2.在你的文檔庫中添加一欄數字列,用來統計被點擊數目

     

3.實現Dispose和Init方法,在Init方法中通過ReleaseRequestState時間截取訪問文檔請求

          public void Init(HttpApplication context)
          {
                 context.ReleaseRequestState += new EventHandler(context_ReleaseRequestState);
          }

4.實現ReleaseRequestState 方法

  

void context_ReleaseRequestState(Object sender, EventArgs e)
        {

                 if(){          //此處截取你需要訪問的請求,一般根據訪問地址的後綴名,如aspx;也有例外,如模式窗體,你需要截取IsDlog=1;必須截取,否則會連圖片樣式請求 一同過濾,導致樣式混亂,圖片不顯示

                        string colName = "瀏覽數";

                        CountIncrement(colName, strUrl);     //兩個參數,數字列的列名和文檔地址(全址)

                }

        }

 

5.實現CountIncrement()方法

//客戶端點擊文檔,添加瀏覽數
        void CountIncrement(string colName, string fileUrl)
        {
            //string webUrl = "";                 //文檔所在網站集
            Guid siteID = SPContext.Current.Site.ID;
            // 以管理員身份運行以下代碼
            Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPSite site = new SPSite(siteID);
                SPWeb web = site.OpenWeb();
                try
                {
                    web.AllowUnsafeUpdates = true;
                    Microsoft.SharePoint.SPFile opeFile = web.GetFile(fileUrl);
                    Microsoft.SharePoint.SPListItem item = opeFile.Item;
                    Microsoft.SharePoint.SPList list = item.ParentList;

                    // 備份值
                    bool allowUnsafeUpdates = web.AllowUnsafeUpdates;
                    bool enabledVersioning = list.EnableVersioning;
                    bool allowForceCheckout = list.ForceCheckout;
                    // 打開站點允許不安全的更新 否則無法更新
                    web.AllowUnsafeUpdates = true;
                    // 關閉版本紀錄 否則每次修改都會產生一個新的版本
                    list.EnableVersioning = false;
                    // 關閉要求籤出纔可以修改文件 否則會要求每次更新時checkout文件
                    list.ForceCheckout = false;
                    list.Update();

                    string internalName = string.Empty;
                    int count = 0;
                    try
                    {
                        //給文件的計數列記錄數值
                        internalName = list.Fields[colName].InternalName;
                        int.TryParse(item[internalName].ToString(), out count);
                        count++;
                        item[internalName] = count;
                        item.Update();
                    }
                    catch { }
                    // 恢復SPWeb和SPList設置
                    web.AllowUnsafeUpdates = allowUnsafeUpdates;
                    list.EnableVersioning = enabledVersioning;
                    list.ForceCheckout = allowForceCheckout;
                    list.Update();
                }
                catch { }
                finally
                {
                    if (web != null)
                        web.Dispose();
                }
            });
        }

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章