Hi Hemendra ,
As u suggested I have created a custom timer job and run it,but don't know somewhere
something is wrong with the code,
Please Help me what is wrong and what nedd to do for the Expiry of the content
Following is the code i have mentioned in the class
namespace CustomTimerJob
{
public class Custom_Timer_Job:SPJobDefinition
{
#region constructors
public Custom_Timer_Job() : base() { }
public Custom_Timer_Job(string jobName, SPService service, SPServer server, SPJobLockType targetType)
: base(jobName, service, server, targetType) { }
public Custom_Timer_Job(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.Job)
{
this.Title = "List Timer Job";
}
#endregion
#region ExcuteMethod
public override void Execute(Guid contentDbId)
{
try
{
SPWebApplication mywebapp = this.Parent as SPWebApplication;
SPSite mysitecol = mywebapp.Sites["http://172.29.17.212:5727/"];
SPList mylist = mysitecol.RootWeb.Lists["Policy"];
SPListItem mytiem = mylist.Items.Add();
mytiem["Title"] = DateTime.Now.ToString();
mytiem.Update();
mylist.Update();
}
catch (Exception e)
{
}
}
#endregion
}
}
--------------------------------------------------------
Following is the Feature I have mentioned for these class
const string List_JOB_NAME = "TimerJobExample";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// Code Logic on Feature Activation
SPWeb wb = properties.Feature.Parent as SPWeb;
if (wb == null)
{
throw new SPException("Error obtaining reference to context Site ");
}
// SPSite site = properties.Feature.Parent as SPSite;
// make sure the job isn't already registered
foreach (SPJobDefinition job in wb.Site.WebApplication.JobDefinitions)
{
if (job.Name == List_JOB_NAME)
job.Delete();
}
Custom_Timer_Job listLoggerJob = new Custom_Timer_Job(List_JOB_NAME, wb.Site.WebApplication);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 5;
listLoggerJob.Schedule = schedule;
listLoggerJob.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
// Code Logic before deactivating the feature recevier
SPWeb wb = properties.Feature.Parent as SPWeb;
// delete the job
foreach (SPJobDefinition job in wb.Site.WebApplication.JobDefinitions)
{
if (job.Name == List_JOB_NAME)
job.Delete();
}
}
No comments:
Post a Comment