山的那一边

导航

moss文档浏览次数统计

在知识管理中,文档的的点击率是个很重要的指标,但MOSS中并没有直接提供这样的功能。Audit提供了类似的功能,但因为他是集中记录在数据库中,不能直接体现在文章的自定义列中,导致一定的不方便。
故这里采用在Global.asax中(HttpModule或者HttpHandler也是一样的)记录的请求的方法来统计点击率。
需要注意的地方:
1.
建立站点之后,站点的根目录自动会创建一个Global.asax文件,这个文件的内容是:
<%@ Assembly Name="Microsoft.SharePoint"%><%@ Application Language="C#" Inherits="Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication" %>
这是不能去掉的,因为这里面调用了MOSS的DLL做了很多相关事情。
2.
因为要访问MOSS站点,因此代码不能写在BeginRequest中,而需要在通过验证之后的某个事件,这里用的是PreRequestHandlerExecute。
3.
因为要访问文档库并要更新相关更新列,有的查看者可能没有权限,所以必须提升权限。在提升权限时要记住重新打开SPWeb,而不能使用Context获取的SPWeb。
4.
如果开启了版本纪录,在更新列的时候会产生新版本,因此需要临时关闭版本纪录,更新之后再恢复。
5.
在AppSettings里面配置用于记录点击次数的列名。
源代码如下:
<%@ Assembly Name="Microsoft.SharePoint"%><%@ Application Language="C#" Inherits="Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication" %>
<script runat="server">
    void Application_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        try
        {
            // Code that runs when an unhandled error occurs
            HttpApplication httpApp = (HttpApplication)sender;
            string colName = System.Configuration.ConfigurationManager.AppSettings["HitsColumnName"];
          
            AddCount(colName, httpApp.Request.Url.AbsoluteUri, HttpContext.Current);
        }
        catch { }

    }
   void AddCount(string colName, string fileUrl, HttpContext content)
    {
        Microsoft.SharePoint.SPWeb contextWeb = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(content);
        Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                Microsoft.SharePoint.SPSite site = null;
                Microsoft.SharePoint.SPWeb web = null;
                site = new Microsoft.SharePoint.SPSite(contextWeb.Site.Url);
                web = site.OpenWeb();           
               
                try
                {             
                 
                    Microsoft.SharePoint.SPFile file = web.GetFile(fileUrl);

                    Microsoft.SharePoint.SPListItem item = file.Item;
                    Microsoft.SharePoint.SPList list = item.ParentList;
                    bool enabled = list.EnableVersioning;
                    //关闭版本纪录
                    web.AllowUnsafeUpdates = true;
                    list.EnableVersioning = false;
                    list.Update();
                    string internalName = string.Empty;
                    int count = 0;
                    try
                    {
                        internalName = list.Fields[colName].InternalName;
                        try
                        {
                            count = Convert.ToInt32(item[internalName]);
                        }
                        catch { }
                        count++;
                        item[internalName] = count;
                        item.Update();
                    }
                    catch { }

                    //恢复版本纪录
                    list.EnableVersioning = enabled;
                    list.Update();
                }
                catch (Exception ex)
                { }
                finally
                {
                   if (site != null)
                        site.Dispose();
                    if (web != null)
                        web.Dispose();
                }
            });
    }
      
</script>
使用方法:
1.把以上代码保存为Global.assx放在站点根目录
2.在web.config的appSettings中添加HitsColumnName的配置
3.在需要统计点击率的文档库中添加对应的列,类型为数字。

posted on 2009-09-13 14:54  George Zhang  阅读(854)  评论(1)    收藏  举报