欧非资源网:安全、免费、专业放心的资源下载站! 最新软件|软件分类

您的位置:欧非资源网 > 其他专区 > SharePoint > sharepoint 2010 如何利用timer job 实现文档下载次数

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

时间:2020-09-03 21:52作者:admin来源:未知人气:291我要评论(0)

最近在做文档库设计的时候,公司需要统计用户对文档的下载次数这个功能,在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,整个过程就完成了。

相关阅读 SharePoint中Office文件无法打开的解决方案如何快速生成SharePoint测试大文件如何查询SharePoint Library中空文件夹?如何快速备份SharePoint Farm Solution如何解决Event Viewer中SharePoint Error - Event ID 8321SharePoint 2016 CU安装失败,"Exception: The upgraded database schema doesn't match the TargetSchema"的解决方案InfoPath Error “此文档库已经被重命名或删除,或者网络问题导致文件无法保存…” 的解决方案SharePoint 2013 App概述How to Shrink SharePoint Content Database Log File?Project Web App Feature无法开启的解决方案

文章评论
发表评论

热门文章 SharePoint 2016 图文安装教程 后面有激活序列号、密钥分享[SharePoint入门教程]一SharePoint发展、工具及术语如何用 SharePoint Online创建团队网站?SharePoint Iframe 报错“此内容不能显示在一个框架中”

最新文章 SharePoint中Office文件无法打开的解决方案如何快速生成SharePoint测试大文件 如何查询SharePoint Library中空文件夹?如何快速备份SharePoint Farm Solution如何解决Event Viewer中SharePoint Error - Event ID 8321SharePoint 2016 CU安装失败,"Exception: The upgraded database s

人气排行 SharePoint 2016 图文安装教程 后面有激活序列号、密钥分享[SharePoint入门教程]一SharePoint发展、工具及术语如何用 SharePoint Online创建团队网站?SharePoint Iframe 报错“此内容不能显示在一个框架中”SharePoint 2013 安装图解 SharePoint安装步骤图解教程SharePoint 如何开启访问请求[SharePoint入门教程]一SharePoint概述[SharePoint入门教程]一创建SharePoint母版页

盖楼回复X

(您的评论需要经过审核才能显示)