0% found this document useful (0 votes)
48 views2 pages

LabCodeSnippets Ex3

The document outlines the creation and deployment of a timer job for managing expense data in a SharePoint environment. It includes the implementation of the job's constructor, execution logic for aggregating expenses from multiple sites, and a method for deleting existing job definitions. Additionally, it details the scheduling of the timer job to run at specified intervals.

Uploaded by

aemopc18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views2 pages

LabCodeSnippets Ex3

The document outlines the creation and deployment of a timer job for managing expense data in a SharePoint environment. It includes the implementation of the job's constructor, execution logic for aggregating expenses from multiple sites, and a method for deleting existing job definitions. Additionally, it details the scheduling of the timer job to run at specified intervals.

Uploaded by

aemopc18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

******** 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);

You might also like