• moss文档浏览次数统计之兄弟篇--哈哈,列表项的浏览次数统计


    看到这篇文章 moss文档浏览次数统计 ,忍不住想到了这篇文章: WSS列表访问统计的实现 --编写一个custom field,在render的时候读取字段值并判断加1写回,当然如果需要你也可以做成针对每个用户的统计以及防刷新等。

    但里面的代码有bug--权限提升有问题,对ListItem没修改权限的用户访问会出错。
    修改后的完整代码如下:
    ItemViewCounterField.cs

    public class ItemViewCounterField : SPField
        
    {
           
    public ItemViewCounterField(SPFieldCollection fields, string fieldName)
                : 
    base( fields , fieldName )
            
    {
                Init();
            }


            
    public ItemViewCounterField(SPFieldCollection fields, string typeName, string displayName)
                : 
    base(fields, typeName, displayName)
            
    {
                Init();
            }


            
    void Init()
            
    {
                
    //this.ReadOnlyField
                this.ShowInDisplayForm = true;
                
    this.ShowInEditForm = false;
                
    this.ShowInNewForm = false;            
            }


            
    public override BaseFieldControl FieldRenderingControl
            
    {
                
    get
                
    {
                    BaseFieldControl ctl 
    = new ItemViewCounterFieldControl();
                    ctl.FieldName 
    = this.InternalName;
                    
    return ctl ;
                }

            }

        }

    ItemViewCounterFieldControl.cs
    class ItemViewCounterFieldControl : BaseFieldControl
        
    {
            
    /// <summary>
            
    /// 更新字段
            
    /// </summary>

            void UpdateWithElevatedPrivileges()
            
    {
                 SPSecurity.RunWithElevatedPrivileges(
    delegate()
                    
    {
                        
    using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                        
    {
                            
    using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
                            
    {
                                web.AllowUnsafeUpdates 
    = true;

                                SPList list 
    = web.Lists[ this.ListId ];

                                SPListItem item 
    = list.GetItemById(this.ItemId);

                                
    if (item == nullreturn;

                                item[
    this.FieldName] = this.ItemFieldValue;

                                item.SystemUpdate();
                            }

                        }

                    }

                    );
            }


            
    protected override void Render(HtmlTextWriter output)
            
    {
                
    int cCounter;

                
    if (this.ItemFieldValue == null)
                
    {
                    cCounter 
    = 1;
                }

                
    else
                
    {
                    cCounter 
    = Convert.ToInt32(this.ItemFieldValue);
                    cCounter
    ++;
                }


                
    this.ItemFieldValue = cCounter.ToString();

                
    this.UpdateWithElevatedPrivileges();

                
    if (this.ItemFieldValue == null)
                    output.Write(
    "0");
                
    else
                    output.Write(
    this.ItemFieldValue.ToString());

            }

     
        }

    fldtypes_itemViewCounter.xml


  • 相关阅读:
    Linux input子系统学习总结(一)---- 三个重要的结构体
    DRM/KMS 基本组件介绍
    Framebuffer 驱动学习总结(二)---- Framebuffer模块初始化
    Framebuffer 驱动学习总结(一) ---- 总体架构及关键结构体
    Linux USB驱动学习总结(三)---- USB鼠标的加载、初始化和通信过程
    Linux USB驱动学习总结(一)---- USB基本概念及驱动架构
    使用Python调用动态库
    使用 SignalR与SSE(Sever sent event)向客户端推送提示信息
    在IDEA下使用Spring Boot的热加载(Hotswap)
    使用Spring boot + jQuery上传文件(kotlin)
  • 原文地址:https://www.cnblogs.com/jianyi0115/p/1029915.html
Copyright © 2020-2023  润新知