Thankyou Jeff for the Info.
1) I did install and activate the features using STSADM commands.
2) The Timer job does not appear under Job Definitions in sharepoint manager. The feature however does appear under the Feature definitions in sharepoint manager.
Please find my code below.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Data;
using System.Data.SqlClient;
namespace Reminder.FeatureCode
{
public class Test:SPJobDefinition
{
private string _formStatus = string.Empty;
public Test():base()
{
}
public Test(string jobName, SPService service, SPServer server, SPJobLockType targetType): base(jobName, service, server, targetType)
{
this.Title = "Timer Job";
}
public Test(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
{
this.Title = "Timer Job";
}
private void SendEmail(string EmailBody, string Subject, string ToEmailAddress)
{
try
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.To.Add(ToEmailAddress);
msg.From = new System.Net.Mail.MailAddress("****");
msg.Subject = Subject;
msg.IsBodyHtml = true;
//EmailBody = " <b>Welcome to Send an Email!!</b><p> Example.<BR>";
msg.Body = EmailBody;
string smtp = "*****";
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(smtp);
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "**";
NetworkCred.Password = "**";
NetworkCred.Domain = "**";
client.Credentials = NetworkCred;
client.Send(msg);
}
catch (Exception ex)
{
LogError(ex.InnerException.ToString(), ex.StackTrace);
}
}
public override void Execute(Guid contentDbId)
{
System.Diagnostics.Debug.Assert(false);
System.Diagnostics.Debug.WriteLine(SPContext.Current.Site.WebApplication.Name);
StringBuilder _emailBody = new StringBuilder();
string _staffName=string.Empty;
string _Emailtext=string.Empty;
string _url =string.Empty;
string _formNo = string.Empty;
string _staffEmailAddress = string.Empty;
try
{
string url = @"http://url";
using (SPSite site = new SPSite(url))
{
using (SPWeb web = site.OpenWeb())
{
SPListItemCollection coll = web.Lists["test"].Items;
foreach (SPListItem item in coll)
{
if (!string.IsNullOrEmpty(_formStatus))
{
if (_formStatus == "Submitted")
{
_emailBody.Append("<b>Welcome to Send an Email!!</b><p> Example.<BR>");
SendEmail(_emailBody.ToString(), _formStatus,_staffEmailAddress);
}
}
}
}
}
}
catch (Exception ex)
{
LogError(ex.Source, ex.StackTrace + ex.InnerException.ToString());
}
//TODO: write here the code of your job!
}
}
public class TimerJob : SPFeatureReceiver
{
const string TASK_LOGGER_JOB_NAME = "Timer Job";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
//SPDiagnosticsService diag = new SPDiagnosticsService();
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = properties.Feature.Parent as SPSite;
// make sure the job isn't already registered
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == TASK_LOGGER_JOB_NAME)
job.Delete();
}
// install the job
Reminder.FeatureCode.Test taskLoggerJob = new Reminder.FeatureCode.Test(TASK_LOGGER_JOB_NAME, site.WebApplication);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 2;
//SPSchedule customSchedule = SPSchedule.FromString("daily at 01:45");
taskLoggerJob.Schedule = schedule;
taskLoggerJob.Update();
});
}
catch (Exception ex)
{
LogError(ex.InnerException.ToString(), ex.StackTrace);
}
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
// delete the job
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == TASK_LOGGER_JOB_NAME)
job.Delete();
}
}
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
}
}
}
Feature <?xml version="1.0" encoding="utf-8"?>
<Feature Id="f9101141-7463-49e3-911a-9e3a3b7f642f"
Title="Timer Job"
Description="Timer Job"
Version="1.0.0.0"
Hidden="FALSE"
Scope="Farm"
ActivateOnDefault="TRUE"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="elements.xml"/>
</ElementManifests>
</Feature>
enter code here