error While Creating custom timer job

405 Views Asked by At

I am creating the custom timer job, but getting error as Object reference not set to an instance of an object. below is my code....can any one help on this...this code is for removing items from list

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

There are 1 best solutions below

0
AndreyBizin On
  1. Have you tried to Debug it? (restart service after deployment, attach to the owstimer proces..)
  2. I use different approach in getting required site: 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; ... } }