我正在创建自定义计时器作业,但错误为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();
}
}
}
}
答案 0 :(得分:0)
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;
...
}
}