创建自定义计时器作业

时间:2015-08-11 05:50:17

标签: c# sharepoint-2010 timer-jobs

我正在创建自定义计时器作业,但错误为Object reference not set to an instance of an object。 下面是我的代码....可以任何一个帮助...此代码用于从列表中删除项目

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace WebAnalyticsTimerjob
{
    public class WebAnalyticsReport : SPJobDefinition
    {
        public const string JobName = "WebAnalyticsReport";
        public WebAnalyticsReport()
            : base()
        {

        }

        public WebAnalyticsReport(string jobName, SPService service, SPServer server, SPJobLockType lockType)
            : base(jobName, service, server, lockType)
        {
            this.Title = "WebAnalyticsReport";
        }
        public WebAnalyticsReport(string jobName, SPWebApplication webapp)
            : base(jobName, webapp, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "WebAnalyticsReport";
        }
        public override void Execute(Guid targetInstanceId)
        {

            //SPSite mySite = this.Parent as SPSite;
            SPWebApplication webApp = this.Parent as SPWebApplication;
            SPList taskList = webApp.Sites[0].RootWeb.Lists["Tasks"];
            SPListItemCollection listItems = taskList.Lists.TryGetList("Web Analytics Report List").Items;
            if (listItems != null)
            {
                for (int i = listItems.Count - 1; i >= 0; i--)
                {
                    listItems[i].Delete();
                }
            } 
      }    
}

1 个答案:

答案 0 :(得分:0)

  1. 您是否尝试过调试? (部署后重新启动服务,附加到owstimer进程..)
  2. 我使用不同的方法获取所需的网站: public override void Execute(Guid contentDbId) { var webApplication = Parent as SPWebApplication; if (webApplication == null) return; using (var site = webApplication.ContentDatabases[contentDbId].Sites[0]) { var web = site.RootWeb; ... } }