参考《moss文档浏览次数统计》一文,cnblogs 上 风中的猪儿 所写。我按文中方法实现存在很多问题,我进行了改进。
具体步骤:
1.先为你的文档建立一个数字列,叫“ClickRate”。
2.新建一个类库,起名叫HitsCount,添加引用Microsoft.SharePoint,System.configuration以及System.Web
然后,将Class1.cs改名为HitsCountModule.cs
3. HitsCountModule.cs中内容如下
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web;
using System.Configuration;
using Microsoft.SharePoint.WebControls;
namespace HitsCount
{
public class HitsCountModule:IHttpModule
{
List<string> fileTypes;
public void Init(HttpApplication context)
{
context.ResolveRequestCache += new EventHandler(context_ResolveRequestCache);
}
public void Dispose()
{
}
void context_ResolveRequestCache(object sender, EventArgs e)
{
Try
{
HttpApplication httpApp = (HttpApplication)sender;
// 过滤不必要的请求
fileTypes = GetFileTypes();
if (!FiltrateFile(httpApp.Request.Url.AbsoluteUri))
{
return;
}
// 从配置文件中获得计数器列的名称
string colName = System.Configuration.ConfigurationManager.AppSettings ["HitsColumnName"].ToString();
CountIncrement(colName,httpApp.Request.Url.AbsoluteUri, HttpContext.Current);
}
catch { }
}
void CountIncrement(string colName,string fileUrl, HttpContext content)
{
SPWeb contextWeb = SPContext.Current.Web;
// 以管理员身份运行以下代码
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
{
try
{
Microsoft.SharePoint.SPFile file = web.GetFile(fileUrl);
Microsoft.SharePoint.SPListItem item = file.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(Convert.ToString(item[internalName]), out count);
count++;
item[internalName] = count;
item.SystemUpdate();
}
catch (Exception exErr)
{
}
// 恢复SPWeb和SPList设置
list.EnableVersioning = enabledVersioning;
list.ForceCheckout = allowForceCheckout;
list.Update();
web.AllowUnsafeUpdates = allowUnsafeUpdates;
}
catch (Exception exErrOut)
{
}
Finally
{
if (web != null)
web.Dispose();
}
}
}
});
}
// 获取支持文件类型
List<string> GetFileTypes()
{
string typeString = System.Configuration.ConfigurationManager.AppSettings["typeString"].ToString(); // 也可以从配置文件读出来
List<string> types = new List<string>();
types.InsertRange(0, typeString.Split(','));
return types;
}
// 验证是否是支持的文件类型
bool FiltrateFile(string fileUrl)
{
string s = fileUrl.Substring(fileUrl.LastIndexOf('.'));
return fileTypes.Contains(s);
}
}
}
4.建立强名称。右键项目-》属性-》签名-》把为程序集签名勾上,然后在下面下拉菜单中点“新建”,在弹出来的对话框中把“使用密码保护密钥文件”的小勾去掉,随便输入一个名字,确定编辑。
5.把编译好的dll拖入c:/windows/assembly目录下。
6. 修改web.config文件,我的是在D:/Inetpub/wwwroot/wss/VirtualDirectories/80下,查找<httpModules>,在此节点下添加 <add name="HitsCount" type="HitsCount.HitsCountModule, HitsCount, Version=1.0.0.0, Culture=neutral, PublicKeyToken=239ba512823b3816"/>
其中PublicKeyToken值会不太一样,需要修改,可以在c:/windows/assembly下找到你拖进去的那个dll查属性就可以看到了。要么你就用Reflector来看吧
在节点<appSettings> 下添加 AppSetting, <add key="HitsColumnName" value="ClickRate" />
<add key ="typeString" value=".docx,.doc,.txt,.pdf,.xls,.jpg,.gif,.xml,.xlsx,.ex_" />
完了后,在命令行输入iisreset重启IIS就完成了。