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();
                }
            });
        }

 

 

你可能感兴趣的:(object,String,文档,SharePoint,HttpModule,2010)