MOSS文档浏览次数统计

 参考《MOSS中文档点击率统计的实现方法》一文,具体出处不太清楚,在网上搜到的,都没有标明出处或是原著,我也就不给出链接了,大家可以自己去搜一下。我按文中方法实现不成功,按照文中思路重新新写了一遍,原文中是通过修改global.asax文件我发现这样没法调试(至少我这里没法调试,我这里总有点问题样的,工作流也没法附加进程调试,哪位高手能指定我一下为什么),于是改用httpmodule的方式,即可以实现,又可以调试。

具体步骤:

1.先为你的文档建立一个数字列,叫“浏览数”。

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 system.web;
using microsoft.sharepoint;
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"];
                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()
                
{
                    spweb web 
= spcontext.current.web;
                    
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(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();
                    }

                }
);
        }

        
// 获取支持文件类型
        list<string> getfiletypes()
        
{
            
string typestring = ".docx,.doc";   // 也可以从配置文件读出来
            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);
        }

    }

}

 

原文是用prerequesthandlerexecute我用prerequesthandlerexecute是捕获不到我用word打开的.docx文件,于是我改用resolverequestcache方法后,解决。

4.建立强名称。右键项目-》属性-》签名-》把为程序集签名勾上,然后在下面下拉菜单中点“新建”,在弹出来的对话框中把“使用密码保护密钥文件”的小勾去掉,随便输入一个名字,确定。编译

5. 把编译好的dll拖入c:\windows\assembly目录下。也就是加入gac,我是建立了一个bat文件,部署方便,bat文件内容如下

"%programfiles%\microsoft visual studio 8\sdk\v2.0\bin\gacutil.exe" -uf hitscount
"%programfiles%\microsoft visual studio 8\sdk\v2.0\bin\gacutil.exe" -if bin\debug\hitscount.dll

iisreset

 

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来看吧。

 

完了后,在命令行输入iisreset重启iis就完成了。

 

如果需要调试,直接在vs里,调试-》附加进程-》然后选择w3wp.exe。就可以设置断点并调试了。如果有多个w3wp.exe,可以在命令行下用iisapp命令来识别。

转自:http://www.cnblogs.com/pengyuan512/archive/2008/01/07/1028522.html

原文地址:https://www.cnblogs.com/chenfulai/p/1407814.html