zoukankan      html  css  js  c++  java
  • 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


  • 相关阅读:
    手撕面试官系列(十一):BAT面试必备之常问85题
    手撕面试官系列(十):面试必备之常问Dubbo29题+MySQL55题
    手撕面试官系列(九):分布式限流面试专题 Nginx+zookeeper
    手撕面试官系列(八):分布式通讯ActiveMQ+RabbitMQ+Kafka面试专题
    手撕面试官系列(七):面试必备之常问并发编程高级面试专题
    手撕面试官系列(六):并发+Netty+JVM+Linux面试专题
    手撕面试官系列(五):Tomcat+Mysql+设计模式面试专题
    手撕面试官系列(四 ):MongoDB+Redis 面试专题
    手撕面试官系列(三):微服务架构Dubbo+Spring Boot+Spring Cloud
    linux 使用socket代理
  • 原文地址:https://www.cnblogs.com/jianyi0115/p/1029915.html
Copyright © 2011-2022 走看看