• moss文档浏览次数统计(转)


    改用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命令来识别。

     

    总结

    这个方法虽然实现了浏览数的统计,可是也有一个问题,就是我们的“浏览数”是一个普通的列,用户只要有一定的权限就能修改这个列的值。

  • 相关阅读:
    javaWeb快速入门+——初体验-HelloWorld
    简单理解Linux系统的挂载是什么鬼
    STM32MP157 Cortex®-M4高性能系列MCU
    常见六种锂电池特性及参数对比
    IIC通信详解
    stm32微秒延时问题
    STM32 HAL库实现微秒级别延时
    开关电源波纹的产生、测量及抑制
    图解DIY 1pA超微电流测试器
    stm32
  • 原文地址:https://www.cnblogs.com/Anlycp/p/1210434.html
Copyright © 2020-2023  润新知