******** Ex 3, Task 2: Create a timer job definition ********
******** Step 6 ********
public ContosoExpensesOverviewTimerJob()
{
}
******** Ex 3, Task 2: Create a timer job definition ********
******** Step 7 ********
public ContosoExpensesOverviewTimerJob(string name, SPWebApplication
webApplication, SPServer server, SPJobLockType lockType)
: base(name, webApplication, server, lockType)
{
}
******** Ex 3, Task 2: Create a timer job definition ********
******** Step 8 ********
public override void Execute(Guid targetInstanceId)
{
// Obtain a reference to the managers site collection.
using (SPSite managerSite = new SPSite("[Link]
{
// Obtain a reference to the managers site.
using (SPWeb managerWeb = [Link])
{
// Obtain a reference to the expense overview list.
SPList overviewList = [Link]["Expenses Overview"];
// Remove all existing items from the list.
while ([Link] > 0)
{
[Link][0].Delete();
[Link]();
}
// Iterate through each site collection in the current web application.
foreach (SPSite departmentSite in [Link])
{
using (SPWeb departmentWeb = [Link])
{
// Get the Contoso Expenses list, if one exists.
SPList expensesList = [Link]("Contoso
Expenses");
if (expensesList != null)
{
// Calculate the total for the department.
double departmentTotal = 0;
foreach (SPListItem expense in [Link]["Contoso
Expenses"].Items)
{
departmentTotal += (double)expense["InvoiceTotal"];
}
// Use the site URL to determine the department name.
Uri url = new Uri([Link]);
string hostName = [Link]([Link],
[Link]);
string[] hostNameComponents = [Link]('.');
// Create a new item in the expense overview list.
SPListItem overviewItem = [Link]();
overviewItem["Title"] = hostNameComponents[0];
overviewItem["Expense Total"] = departmentTotal;
[Link]();
[Link]();
}
}
[Link]();
}
}
}
}
******** Ex 3, Task 3: Deploy the timer job ********
******** Step 9 ********
private void deleteJob(SPWebApplication webApplication)
{
foreach(SPJobDefinition job in [Link])
{
if([Link](timerJobName))
{
[Link]();
}
}
}
******** Ex 3, Task 3: Deploy the timer job ********
******** Step 11 ********
SPWebApplication webApplication =
((SPSite)[Link]).WebApplication;
deleteJob(webApplication);
ContosoExpensesOverviewTimerJob timerJob = new
ContosoExpensesOverviewTimerJob(timerJobName,webApplication, null,
[Link]);
SPMinuteSchedule schedule = new SPMinuteSchedule();
[Link] = 1;
[Link] = 5;
[Link] = 2;
[Link] = schedule;
[Link]();
******** Ex 3, Task 3: Deploy the timer job ********
******** Step 13 ********
SPWebApplication webApplication =
((SPSite)[Link]).WebApplication;
deleteJob(webApplication);