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里面配置用于记录点击次数的列名。
源代码如下:
1. <%@ Assembly Name="Microsoft.SharePoint"%><%@ Application Language="C#" Inherits="Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication" %>
2. <script runat="server">
3. void Application_PreRequestHandlerExecute(object sender, EventArgs e)
4. {
5. try
6. {
7. // Code that runs when an unhandled error occurs
8. HttpApplication httpApp = (HttpApplication)sender;
9. string colName = System.Configuration.ConfigurationManager.AppSettings["HitsColumnName"];
10.
11. AddCount(colName, httpApp.Request.Url.AbsoluteUri, HttpContext.Current);
12. }
13. catch { }
14. }
15. void AddCount(string colName, string fileUrl, HttpContext content)
16. {
17. Microsoft.SharePoint.SPWeb contextWeb = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(content);
18. Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
19. {
20. Microsoft.SharePoint.SPSite site = null;
21. Microsoft.SharePoint.SPWeb web = null;
22. site = new Microsoft.SharePoint.SPSite(contextWeb.Site.Url);
23. web = site.OpenWeb();
24.
25. try
26. {
27.
28. Microsoft.SharePoint.SPFile file = web.GetFile(fileUrl);
29. ListItem item = file.Item;
30. Microsoft.SharePoint.SPList list = item.ParentList;
31. bool enabled = list.EnableVersioning;
32. //关闭版本纪录
33. web.AllowUnsafeUpdates = true;
34. list.EnableVersioning = false;
35. list.Update();
36. string internalName = string.Empty;
37. int count = 0;
38. try
39. {
40. internalName = list.Fields[colName].InternalName;
41. try
42. {
43. count = Convert.ToInt32(item[internalName]);
44. }
45. catch { }
46. count++;
47. item[internalName] = count;
48. item.Update();
49. }
50. catch { }
51. //恢复版本纪录
52. list.EnableVersioning = enabled;
53. list.Update();
54. }
55. catch (Exception ex)
56. { }
57. finally
58. {
59. if (site != null)
60. site.Dispose();
61. if (web != null)
62. web.Dispose();
63. }
64. });
65. }
66.
67. </script>
复制代码
使用方法: 1.把以上代码保存为Global.assx放在站点根目录 2.在web.config的appSettings中添加HitsColumnName的配置 3.在需要统计点击率的文档库中添加对应的列,类型为数字。
参照上面的步骤修改了对应的Global.assx和web.config文件,并在文档库中添加对应的列。 尝试后发现,这样配置只能统计根站点下的文档点击率,如果是在下一级的子站点就没有效果。 调试后发现,原因在于第24行 24 web = site.OpenWeb(); 这里获得的SPWEB对象是根站点的。 而第18行中contextweb对象获取的才是当前站点的SPWEB对象 18 Microsoft.SharePoint.SPWeb contextWeb = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(content); 于是直接修改成 web = site.OpenWeb(contextWeb.Url), 结果发现这里需要相对路径,于是修改为 web = site.OpenWeb(contextWeb.ServerRelativeUrl),成功。 有个疑问就是既然contextweb获取的是当前站点对象,为什么还要再定义site和web对象,感觉有点重复。 尝试把这一段注释掉,就诶过就会报错。。 [思路: 在点击的时候调用AddCount事件,HitsNumber自增] |