Hello, I am new in developing timer jobs in SharePoint. Can anyone tell the scenario or refer to links for creating a timer job for the process mentioned below:
There are two list 1st is Custom list and 2nd is Task List.
Now we put entries in Custom List whatever required in Task List and added one more extra field of "TaskCreated" of Yes/No type. With this custom list timer job should create automatically task in Task List for all those entries in Custom List which have "TaskCreated" field to be false(No) and then update "TaskCreated" field for that property to be true. And when the task is completed from the task list then that task entry in the Custom Task List should be have "TaskCreated" field false, so that it can recreate the task.
For this I have written the below code:
MyTaskListTimerJob.cs
public class MyTaskListTimerJob : SPJobDefinition
{
public MyTaskListTimerJob ()
: base(){
}
public MyTaskListTimerJob (string jobName, SPService service, SPServer server, SPJobLockType targetType)
: base (jobName, service, server, targetType) {
}
public MyTaskListTimerJob(string jobName, SPWebApplication webApplication)
: base (jobName, webApplication, null, SPJobLockType.ContentDatabase) {
this.Title = jobName;
}
public override string DisplayName
{
get
{
return "Custom - Create my schedule tasks";
}
}
public override string Description
{
get
{
return "Custom Create my schedule task for the users from the Custom Task List.";
}
}
public override void Execute (Guid contentDbId)
{
// get a reference to the current site collection's content database
SPWebApplication webApplication = this.Parent as SPWebApplication;
SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];
CreateTask(contentDb);
}
private void CreateTask(SPContentDatabase contentDb)
{
try
{
// get a reference to the "Task Process Details" list in the RootWeb of the first site collection in the content database
SPList TaskProcessList = contentDb.Sites[0].RootWeb.Lists["TaskListDetails"];
// get a reference to the "Tasks" list in the RootWeb of the first site collection in the content database
SPList taskList = contentDb.Sites[0].RootWeb.Lists["ScheduledTasks"];
////Check to create new schedule tasks
foreach (SPListItem item in TaskProcessList.Items)
{
bool taskcreated = (bool)item["TaskCreated"];
if (!taskcreated)
{
// create a new task, set the Title to the current day/time, and update the item
SPListItem newTask = taskList.Items.Add();
newTask["Task Name"] = item.Title + "_" + DateTime.Now.ToString();
newTask["Start Date"] = DateTime.Now.ToString();
newTask["Due Date"] = item["Due Date"];
newTask["Assigned To"] = item["Assigned To"];
newTask["Status"] = "Not Started";
newTask.Update();
item["TaskCreated"] = true;
item.Update();
}
}
//Check if tasks completed the update TaskDetails Table for TaskCreated=false
foreach (SPListItem Taskitem in taskList.Items)
{
string status = (string)Taskitem["Status"];
if (status == "Completed")
{
// create a new task, set the Title to the current day/time, and update the item
string TaskName = (string)Taskitem["Task Name"];
if (TaskName.IndexOf("_") != -1)
{
TaskName = TaskName.Substring(0, TaskName.IndexOf('_'));
}
foreach (SPListItem TPList in TaskProcessList.Items)
{
if (TaskName == TPList.Title)
{
TPList["TaskCreated"] = false;
TPList.Update();
break;
}
}
}
}
}
catch (Exception ex)
{
}
}
}
Feature1EventReceiver.cs
public class Feature1EventReceiver : SPFeatureReceiver
{
const string TASK_CREATOR_JOB_NAME = "TaskCreator";
const string TASKDETAIL_UPDATE_JOB_NAME = "TaskDetailUpdate";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
DeleteJob(site); // Delete Job if already Exists
CreateTaskJob(site); // Create new Job
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
DeleteJob(properties.Feature.Parent as SPSite); // Delete the Job
}
private static void DeleteJob(SPSite site)
{
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
if (job.Name == TASK_CREATOR_JOB_NAME || job.Name == TASKDETAIL_UPDATE_JOB_NAME)
job.Delete();
}
private static void CreateTaskJob(SPSite site)
{
MyTaskListTimerJob job = new MyTaskListTimerJob(TASK_CREATOR_JOB_NAME, site.WebApplication);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 30;
job.Schedule = schedule;
job.Update();
}
}
I the above code, in MyTaskListTimerJob.cs file the code which is bold is not working? Can anyone suggest about it.
Thanks in advance.