SharePoint 2010 Custom Timer Job with description running for a specific site collection

Writing a custom timer job is not as difficult as you might expect. But you should pay attention to some specific settings if you want to

  • use a custom timer job only for a specific site collection or list
  • use parameters which are set at activation time
  • use a custom description

So i would like to share my impressions with you but i won’t write a whole instruction cause there are a lot in the web of it. I will give a some resources.

A Complete Guide to Writing Timer Jobs in SharePoint 2010

Creating Custom Timer Job in SharePoint 2010

And of course the post on which i basically learned from: Setting the “Job Description” of a Custom SharePoint Timer Job. Fine, let’s start with the custom description.

Custom Description:

If you open your Central Administration and go to Monitoring – Review Job Definitions, you’ll find your job. If you click on it you can edit the properties like  the scheduling. Normally you cannot add your own description like in the screenshot below:

To do so, you have to open your code file for the timer job. In this code lines, you have to add the following codelines:



//Custom Job Description
private string CustomDescription
{
get;
set;
}

// Override built-in Description
public override string Description
{
get
{ return CustomDescription; }
}


What you are doing here is that you creae your custom description and override the default description with your custom one. And it is important that you add

this.title = “My Custom Job”;
this.CustomDescription = ” My custom Description”;

to all of your constructors (there should be 3!). Now you can deploy it. But it won’t work till you have reset your iis. But it is working. Ok, let’s see how we can use properties / parameters for your timer job.

Using Paramaters

In your feature activation code you have to add properties to your timer job. I do it with this line:
centralArchiveJob.Properties.Add(“SiteCollectionUrl”, site.Url);
This adds the property SiteCollectionUrl to my timer job. I will call it in the next step when i use it for a special sitecollection.



public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == JOB_NAME)
job.Delete();
}

CentralArchiveJob centralArchiveJob = new CentralArchiveJob(JOB_NAME, site.WebApplication,null, SPJobLockType.Job);
centralArchiveJob.Properties.Add("SiteCollectionUrl", site.Url);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 5;
centralArchiveJob.Schedule = schedule;
centralArchiveJob.Update();

}


Specific Site Collection

At this point we look at two points. At first we can call the property we specified before with
this.Properties[“SiteCollectionUrl”].ToString()
This we can use if we want to use only the site collection in which the feature is activated. I am not sure what happens if you use the feature in two site collections and only having one timer job. If you checked if let me know.

To target a specific site collection we make use of the “using(SPSite…)” In this case you’ll be able to call Lists and libraries and other things which are in this site collection.



public override void Execute(Guid targetInstanceId)
{
//Execute Timer Job Tasks
//SPWebApplication webApplication = this.Parent as SPWebApplication;
//SPContentDatabase contentDb = webApplication.ContentDatabases[targetInstanceId];

using (SPSite site = new SPSite(this.Properties["SiteCollectionUrl"].ToString()))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists["Tasks"];

SPListItem item = list.Items.Add();
item["Title"] = "TestTimer";
item.Update();
}
}

}


Hope this helps.

..:: I like SharePoint ::..

 

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

Karsten Pohnke About Karsten Pohnke
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.

1 comment on “SharePoint 2010 Custom Timer Job with description running for a specific site collection

  1. I picked up this vodoo somewhere. Is it wrong or do you trust to never serialize?

    ‘You are required to have an empty (parameterless) constructor for serialization
    Public Sub New()
    MyBase.New()
    End Sub

Submit comment

Allowed HTML tags: <a href="http://google.com">google</a> <strong>bold</strong> <em>emphasized</em> <code>code</code> <blockquote>
quote
</blockquote>

Please fill in the captcha: * Time limit is exhausted. Please reload CAPTCHA.