sharepoint 2010 用timer job 兑现列表评论次数

sharepoint 2010 用timer job 实现列表评论次数

继续前面提到的一篇博客,sharepoint 2010 如何给文档库或自定义列表添加评论功能,添加完评论之后,还有个事情,就是需要统计评论数量。这次主要是以文档库为例,给文档库的文档添加评论后,如何在列表中显示当前文档的评论数量。

参考文章:sharepoint 2010 用timer job 实现文档下载次数

                     sharepoint 2010 如何创建一个timer job

sharepoint 2010 用timer job 兑现列表评论次数

思路还是和前面提到的,sharepoint 2010 用timer job 实现文档下载次数,是一样的,也是做一个配置表加一个timer job,定时更新这个评论数。

1。创建一个自定义列表,“评论次数更新表”,字段如下图所示。

sharepoint 2010 用timer job 兑现列表评论次数

其中两个字段,Url 指的是需要更新的列表地址(列表类型不限),UpdateColumn指的是,更新字段的名称。

2。创建一个timer job,类名叫 CommentsCountTimerJob,并且继承SPJobDefinition。

public class CommentsCountTimerJob : SPJobDefinition
    {
      public CommentsCountTimerJob(): base(){}
        public CommentsCountTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
            : base(jobName, service, server, targetType)
        {

        }
        public CommentsCountTimerJob(string jobName, SPWebApplication webApplication)
            : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
        {
            this.Title = jobName;
        }

        public override void Execute(Guid contentDbId)
        {
            int count;
            SPSite site = new SPSite("http://moss:8000");
            SPWeb web = site.OpenWeb();
            web.AllowUnsafeUpdates = true;
            SPServiceContext context = SPServiceContext.GetContext(site);
            SPList DocumentList;
            SocialCommentManager socialCommentManager = new SocialCommentManager(context);
            SPList list = web.Lists.TryGetList("评论次数更新表");
            foreach (SPListItem item in list.Items)
            {
                DocumentList = web.GetList(item["Url"].ToString());
                foreach (SPListItem DocumentItem in DocumentList.Items)
                {
                    count = socialCommentManager.GetCount(new Uri(web.Url + "/" + DocumentItem.Url));
                    string columnName = item["UpdateColumn"].ToString();
                    DocumentItem[columnName] = count;
                    DocumentItem.Update();
                }
              
            }
        }
    }

3。添加一个接收器。

 [Guid("7108753c-c802-4200-a77d-5d54f3fa2567")]
    public class Feature1EventReceiver : SPFeatureReceiver
    {
        const string JOB_NAME = "CommentsCountTimerJob";
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site;
            if (properties.Feature.Parent is SPWeb)
            { site = ((SPWeb)properties.Feature.Parent).Site; }
            else
            { site = (SPSite)properties.Feature.Parent; }
            // make sure the job isn't already registered
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == JOB_NAME)
                {
                    job.Delete();
                }
            }
            // install the job
            CommentsCountTimerJob Com = new CommentsCountTimerJob(JOB_NAME, site.WebApplication);
            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 1;
            Com.Schedule = schedule;
            Com.Update();
        }
    }

4。部署这个timer job。

之后我们返回文档列表,将记事版添加到文档的查看页面,并且添加一些评论,如下图:

sharepoint 2010 用timer job 兑现列表评论次数

再返回到文档列表,等一会评论次数就会更新,将这个文档的评论次数更新为2。并且显示在列表字段中。如下图

sharepoint 2010 用timer job 兑现列表评论次数

其它文档,以及文件夹里面的文档也一样,如果有添加了评论,就会定时更新这个评论次数,到列表中。

其中最主要用到的一点就是,socialCommentManager.GetCount 这个方法,用来统计评论次数。

程序下载地址:http://download.****.net/download/cxx2325938/4913758

广州京微信息科技有限公司,.微软sharepoint解决方案提供商。