sharepoint 2010 用timer job 实现文档下载次数

时间:2023-01-05 08:57:06

最近在做文档库设计的时候,公司需要统计用户对文档的下载次数这个功能,在sharepoint 2010中,有提供了一个叫审核的功能,可以协助我们做到。如果需要统计用户对文档的下载次数,并且在sharepoint的文档列表中显示,考虑到性能的问题,所以采取的方案,就是使用时序timer job,定时去更新文档的下载次数,如下图:

sharepoint 2010 用timer job 实现文档下载次数

 

在开始做这个功能之前,需要启动一个文档的审计功能,点击文档的库设置,信息管理策略设置,选择内容类型(更改源),选择库和文件夹,点击确定,如下图:

sharepoint 2010 用timer job 实现文档下载次数

接下来我们开始实现这个功能。

1。创建一个自定义列表,《下载次数更新表》,包含两个字段,DocumentLibraryUrl,DocumentColumn.这个表示用来存放需要更新的文档库以及所要更新的字段,例如下载次数,当然如果是其它字段也可以。只要和文档库的名称是一样的,就可以。

例如我们需要更新一个文档库的下载次数,我们提交一条数据,如下图所示。

sharepoint 2010 用timer job 实现文档下载次数

 

2。创建一个时序的class文件,DocumentCountJob.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
namespace TimerJobForDocumentDownloadCount
{
    public class DocumentCountJob : SPJobDefinition
    {
       public DocumentCountJob(): base(){}

        public DocumentCountJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
            : base(jobName, service, server, targetType)
        {

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

        public override void Execute(Guid contentDbId)
        {
            SPSite site = new SPSite("http://moss:8000");
            SPWeb web = site.OpenWeb();
            web.AllowUnsafeUpdates = true;
            SPList list = web.Lists.TryGetList("下载次数更新表");
            //需要更新的文档库列表
            SPList DocumentList;
            SPAuditQuery wssQuery = new SPAuditQuery(site);
            int count;
            wssQuery.AddEventRestriction(SPAuditEventType.View);
            foreach (SPListItem item in list.Items)
            {
                DocumentList = web.GetList(item["DocumentLibraryUrl"].ToString());
                foreach (SPListItem DocumentItem in DocumentList.Items)
                {
                    wssQuery.RestrictToListItem(DocumentItem);
                    SPAuditEntryCollection auditCol = web.Audit.GetEntries(wssQuery);
                    count = auditCol.Count;
                    string columnName = item["DocumentColunm"].ToString();
                    DocumentItem[columnName] = count;
                    DocumentItem.Update();
                }
            }
        }
    }
}

3。添加一个feature。DocumentDownloadCount.如下图所示

sharepoint 2010 用timer job 实现文档下载次数

4。添加一个事件接收器。

sharepoint 2010 用timer job 实现文档下载次数

实现这个事件接收器的两个方法函数,在上一篇博客中有详细说明了timer job的用法,具体可以参考

sharepoint 2010 如何创建一个timer job

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
namespace TimerJobForDocumentDownloadCount.Features.DocumentDownloadCount
{
    [Guid("51410494-650b-4e9e-8358-fd8eaa4f5a93")]
    public class DocumentDownloadCountEventReceiver : SPFeatureReceiver
    {
        const string JOB_NAME = "DocumentDownloadCount";
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == JOB_NAME)
                {
                    job.Delete();
                }
            }
            DocumentCountJob Doc = new DocumentCountJob(JOB_NAME, site.WebApplication);
            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 5;
            Doc.Schedule = schedule;
            Doc.Update();
        }
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {

                if (job.Name == JOB_NAME)
                {
                    job.Delete();
                }
            }

        }
    }
}

5。部署这个timer job。部署完成之后,我们在管理中心的作业定义中可以找到,如下图所示

sharepoint 2010 用timer job 实现文档下载次数

点击进去可以配置它的具体运行时间间隔。如下图所示

sharepoint 2010 用timer job 实现文档下载次数

6。最后重启服务sharepoint 2010 timer,整个过程就完成了。

程序下载地址:http://download.csdn.net/detail/cxx2325938/4891841

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