在sharepoint中,所有列表都包含状态列(数据类型为下拉列表)& Duedate 专栏。 Duedate列超过今天的日期状态列应该更改使用Timerjob自动关闭。我使用此代码但无法正常工作请有人修改此代码以及如何实现此解决方案。
class.cs
namespace TimerJobNew
{
class StatusCompleted : SPJobDefinition
{
public const string jobName = "CompletedProjectsJob";
public StatusCompleted() : base() { }
public StatusCompleted(SPWebApplication webApplication)
: base(jobName, webApplication, null,SPJobLockType.Job)
{
Title = "Completed Projects Job";
}
public override void Execute(Guid targetInstanceId)
{
SPWebApplication webApp = this.Parent as SPWebApplication;
SPSite site = new SPSite(SPContext.Current.Web.Url);
SPWeb web = site.RootWeb;
// SPWeb web = webApp.Sites["/sites/test"].RootWeb;
SPList list = web.Lists.TryGetList("CommonList");
SPListItem items;
bool flag =true;
SPListItemCollection itemColl = list.Items;
var query =new SPSiteDataQuery();
query.Lists = "<Lists BaseType='0' />";
query.ViewFields = "<FieldRef Name='Title' Nullable='TRUE' />" +
"<FieldRef Name='Status' Nullable='TRUE' />" +
"<FieldRef Name='CommonlistID' Nullable='TRUE' />";
query.Query = "<Where>" +
"<Eq>" +
"<FieldRef Name='Status' />" +
"<Value Type='Choice'>Closed</Value>" +
"</Eq>" +
"</Where>";
query.Webs = "<Webs Scope='SiteCollection' />";
DataTable dt = web.GetSiteData(query);
foreach (DataRow row in dt.Rows)
{
items = list.Items.Add();
if (itemColl.Count != 0)
{
foreach (SPListItem item in itemColl)
{
if (item["CommonlistID"].ToString() == row["CommonlistID"].ToString())
{
flag = false;
break;
}
else
{
flag = true;
}
}
if (flag ==true)
{
items["Title"] = row["Title"].ToString();
items["Status"] = row["Status"].ToString();
items["CommonlistID"] = row["CommonlistID"];
items.Update();
list.Update();
}
}
else
{
items["Title"] = row["Title"].ToString();
items["Status"] = row["Status"].ToString();
items["CommonlistID"] = row["CommonlistID"];
items.Update();
list.Update();
}
}
}
}
}
[Guid("95a2f297-4d46-45b2-b792-a7874f11ce08")]
public class StatusFeatureEventReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
DeleteJob(webApp.JobDefinitions);
StatusCompleted tasksTimerJob = new StatusCompleted(webApp);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 1;
tasksTimerJob.Schedule = schedule;
tasksTimerJob.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
DeleteJob(webApp.JobDefinitions);
}
private void DeleteJob(SPJobDefinitionCollection jobs)
{
foreach (SPJobDefinition job in jobs)
{
if (job.Name.Equals(StatusCompleted.jobName,
StringComparison.OrdinalIgnoreCase))
{
job.Delete();
}
}
}
}
}
答案 0 :(得分:0)
正如评论中所述,您的代码永远不会工作,因为使用SPTimerJob时SPContext为null。 (注意这与EventReceiver相同)
您可以增强作业的构造函数以检索更多信息。所以,在你的情况下你可以这样做:
public StatusCompleted(SPWebApplication webApplication, string TargetSite)
: base(jobName, webApplication, null,SPJobLockType.Job)
{
Title = "Completed Projects Job";
Properties.Add("TargetSite", TargetSite);
}
然后,当您调用Execute()方法时,您可以检索该站点:
string targetList = (this.Properties["TargetList"] ?? string.Empty).ToString();
using (SPSite site = new SPSite(targetSite))
{
using (SPWeb web = site.OpenWeb())
{
//TO DO
}
}
希望这有帮助
最后想到,不要忘记调试你的TimerJob。找到错误会非常有用。 您只需将Visual Studio附加到进程OWSTIMER.exe。