Wednesday, November 21, 2012

Adding a description to a custom SharePoint timer job

If you've ever dug through a bunch of SharePoint timer jobs installed on a SharePoint farm wondering what the purpose of a given job is as well as why the Description field always seems to be blank for the custom timer jobs, I feel your pain and would like to offer you a potential solution to this problem.


The most likely reason that the Description field is blank for custom timer jobs is because the property associated with it is read-only. To make a long story short, the property only has a "get", but no "set" operation; therefore, the developer of the custom timer job can't set the text of that property directly. To overcome this issue, you or your developer will need to do three things:
  1. Create a custom property, CustomDescription, in the custom timer job's class which will reference a private field, m_description
  2. Override the Description property and have it obtain the value of your custom description's private field, m_description
  3. Set the value of the CustomDescription property in each of the timer job's constructors
Here is some sample code that highlights all of the steps within my custom timer job called EmailNotificationTimerJob as follows:

class EmailNotificationTimerJob : SPJobDefinition
{
    private string m_description;
    public override string Description
    {
        get
        {
            return m_description;
        }
    }

    public string CustomDescription
    {
        get
        {
            return m_description;
        }
        set
        {
            m_description = value;
        }
    }

    public EmailNotificationTimerJob() : base()
    {
        this.Title = "CHCO Doc Retention - Email Notifications";
        this.CustomDescription = "This timer job will be used to send email notifications to users who are responsible for documents that are about to expire";
    }

    public EmailNotificationTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
        : base(jobName, service, server, targetType)
    {
        this.Title = "CHCO Doc Retention - Email Notifications";
        this.CustomDescription = "This timer job will be used to send email notifications to users who are responsible for documents that are about to expire";
    }

    public EmailNotificationTimerJob(string jobName, SPWebApplication webApplication)
        : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
    {
        this.Title = "CHCO Doc Retention - Email Notifications";
        this.CustomDescription = "This timer job will be used to send email notifications to users who are responsible for documents that are about to expire";
    }

    ...

Once you have deployed the timer job code and performed an IISReset on the farm, your SharePoint Admins will now be provided with a description of the custom timer job on the Edit Timer Job page as follows:




No comments:

Post a Comment

Note: Only a member of this blog may post a comment.