SharePoint Events

  5/06/2014 - Webcast: Advanced Workflow, Forms and Mobility in Office 365 and SharePoint 2013 from Nintex
  5/07/2014 - Webcast: View, Annotate and Secure your Content Anywhere and on Any Device in SharePoint 2013
  5/08/2014 - Webcast: Advanced Records Management with SharePoint
  5/29/2014 - Webcast: Enhance Productivity, Usability and Relevance with Advanced Search in SharePoint 2013

 SharePoint Videos

  Why SharePoint 2013
  Keys to Successful SharePoint Initiatives and User Adoption
  Out of the Box Business Intelligence and Reporting in SharePoint 2013
  Driving the Business Case and User Adoption for SharePoint
  Automate Business Processes with SharePoint 2013 and Business Connectivity Services
  SharePoint and Office 365 Migration Made Easy
  Full Lifecycle Content Management with SharePoint 2013
  Compliant Document Generation and Assembly in SharePoint
  Application of Content Security and Corporate Risk Protection in SharePoint
  How to Redline, Markup, Collaborate and Review Content in SharePoint
  Advanced Content Lifecycle and Records Management in SharePoint
  Advanced Workflow Made Easy with SharePoint 2013 and Nintex
  Full Lifecycle Content Management with SharePoint 2013
  SharePoint 2013 Solutions for the Financial Services Industry
  ProjectReady 2013: SharePoint Solutions built for the AEC
  SharePoint 2013 Solutions for the Legal Industry
  Driving the Business Case and User Adoption for SharePoint
  Investment Considerations for SharePoint 2010 and SharePoint 2013
  SharePoint 2013 and Enterprise Content Management
  SharePoint 2013 Launch
(More Links...)

 Archives

Opening SharePoint Links in a new windowUse SHIFT+ENTER to open the menu (new window).
Mail Enabled Lists vs. The Missing Windows 2008 POP3/IMAP Server Use SHIFT+ENTER to open the menu (new window).
7 Tools for SharePoint DevelopersUse SHIFT+ENTER to open the menu (new window).
Public Facing Masterpage TechniquesUse SHIFT+ENTER to open the menu (new window).
How to Quickly Deploy and Activate a Timer Service to Your Site CollectionUse SHIFT+ENTER to open the menu (new window).
Custom SharePoint Master Page Feature with WSP BuilderUse SHIFT+ENTER to open the menu (new window).
Date Math with InfoPathUse SHIFT+ENTER to open the menu (new window).
Enterprise Search Tricks and Tips Part 1Use SHIFT+ENTER to open the menu (new window).
Populating Word Documents With SharePoint Data. Try The DIP!Use SHIFT+ENTER to open the menu (new window).
Programmatic Deep Dive into Blank SharePoint Lookup ColumnsUse SHIFT+ENTER to open the menu (new window).
1 - 10 Next
Creating a SharePoint 2010 Timer Service using Visual Studio 2010

By: Arjun Chakraborty

Introduction:
Some time ago, I wrote an article on how to quickly make a SharePoint 2007 timer service on Visual Studio 2008.  I think it’s only fair to follow up that article with a new article: how to quickly create a Timer service for SharePoint 2010.

In this case, let’s create a simple timer service that finds a predefined site, and changes the a library’s description based on how many items are in that library.  This a pretty simple (perhaps trivial) task, but it quickly demonstrates how to get a timer service running.

Requirements: SharePoint 2010 and Visual Studio 2010 installed on the same machine.

Walkthrough: First, create a new SharePoint 2010 Empty SharePoint Project:

The_SharePointBlog_Creating_a_SharePoint_2010_Timer_Service_Using_Visual_Studio_2010
 

Then, make sure you create this project as a Farm Solution.

The_SharePointBlog_Creating_a_SharePoint_2010_Timer_Service_Using_Visual_Studio_2010

Now, add a Class to the Project.  Name it “ListDescTimer.cs”.  Your Solution Explorer should now look something like this:

The_SharePointBlog_Creating_a_SharePoint_2010_Timer_Service_Using_Visual_Studio_2010

The class within this cs file should inherit from SPJobDefinition.  It also needs 3 methods, 2 constructors, including a default constructor, and an execute method.  In the end, at the very least, you need this:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint.Administration;

using Microsoft.SharePoint;

 

namespace ExampleTimerService

{

    class ListDescTimer : SPJobDefinition

    {

        // Default constructor

        public ListDescTimer()

        {

        }

 

        // Constructor with a controlled name. 

        // This will be handy on deactivate

        public ListDescTimer(SPWebApplication webApp, string JOB_NAME)

            : base(JOB_NAME, webApp, null, SPJobLockType.ContentDatabase)

        {

            this.Title = JOB_NAME;

        }

 

        // This is what happens when the timer service executes.

        public override void Execute(Guid targetInstanceId)

        {

 

        }

 

    }

}

 

Okay, I’d we are already 1/3 of the way there.  Now we need to add a SharePoint Feature to actually deploy and remove the timer service.  This feature will need to be scoped at a web application.  That’s where all the timer services run. 

So, let’s add a feature:

The_SharePointBlog_Creating_a_SharePoint_2010_Timer_Service_Using_Visual_Studio_2010

The_SharePointBlog_Creating_a_SharePoint_2010_Timer_Service_Using_Visual_Studio_2010

We will also use a receiver to make sure the timer service (ListDescTimer.cs) is properly added or removed:

The_SharePointBlog_Creating_a_SharePoint_2010_Timer_Service_Using_Visual_Studio_2010

Inside the EventReceiver.cs code, we need two methods as shown here:

FeatureActivated should remove all older copies of the service, and install a new one.  The FeatureDeactivated method should only remove all copies of this timer service.  I also use a constant to keep track of the service name.

 

    public class Feature1EventReceiver : SPFeatureReceiver

    {

        private const string JOB_NAME = "Timer - List Description";

 

        public override void FeatureActivated

            (SPFeatureReceiverProperties properties)

        {

        }

 

        public override void FeatureDeactivating

            (SPFeatureReceiverProperties properties)

        {

        }

    }

 

The code for these two method generally look something like this:

    public class Feature1EventReceiver : SPFeatureReceiver

    {

        private const string JOB_NAME = "Timer - List Description";

 

        public override void FeatureActivated

            (SPFeatureReceiverProperties properties)

        {

            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

            foreach (SPJobDefinition job in webApp.JobDefinitions)

            {

                if (job.Name == JOB_NAME)

                    job.Delete();

            }

 

            ListDescTimer timerService = new ListDescTimer(webApp, JOB_NAME);

            timerService.Schedule = SPSchedule.FromString("every 5 minutes");

            timerService.Update();

        }

 

        public override void FeatureDeactivating

            (SPFeatureReceiverProperties properties)

        {

            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

            foreach (SPJobDefinition job in webApp.JobDefinitions)

            {

                if (job.Name == JOB_NAME)

                    job.Delete();

            }

        }

    }

 

This feature adds and removes the service to the web application, based on a constant name.  Now, we are 2/3 of the way there.  The only step left is to actually implement the execute method in the Timer Service.  Again, we want to find a particular list, and change its description.

 

        // This is what happens when the timer service executes.

        public override void Execute(Guid targetInstanceId)

        {

            SPSite site = this.WebApplication.Sites["Examples"];

            // "Examples" is the root site's display name.

 

            using (SPWeb web = site.OpenWeb("/"))

            {

                SPList list = web.Lists["Custom List 001"];

                int count = list.ItemCount;

                list.Description =

                    "There are " + count + " items here.";

                list.Update();

            }

        }

 

This completes our brief coding.  Now we simply deploy it using the Visual Studio 2010 Interface:

The_SharePointBlog_Creating_a_SharePoint_2010_Timer_Service_Using_Visual_Studio_2010

Now to activate this feature, we would normally have to go to central administration.  In central administration, we would follow the “Manage web applications” link to the top left:

The_SharePointBlog_Creating_a_SharePoint_2010_Timer_Service_Using_Visual_Studio_2010

Then we would select the correct web application, got to Manage Features, and activate the correct features.

HOWEVER, since we deployed it through visual studio, this is already deployed and activated.  You can check the status of your timer service by going to Monitoring:

Under Timer Jobs, select Review Job Definitions to find your deployed timer services.  The timer services can also be filtered based on a several parameters, including web application.  You can also force the timer service to execute on-the-spot:

Anyway, the result of the previously mentioned code is this:

I hope this helps you create a timer service with confidence.  Good luck!

By: Arjun Chakraborty

        

Comments

Xristian

Hello,when i try to deploy the timer at a second time old code is executed. How do you update the timer code?

Regards,
Cristian
at 5/6/2011 11:46 AM

Arjun

You may need to restart the "SharePoint 2010 Timer" windows service.

Also, make sure any given web application does have multiple copies of this timer service running.  If that is the case, the "FeatureDeactivating" method in the feature receiver may not have been implemented correctly. (deactivating isn't removing the timer service).  Maybe there is a timer job name missmatch?
at 5/9/2011 11:03 AM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Your Name *


e-mail address *


Website (optional)


Comment *


Attachments

 Subscribe

  GigWerks RSS  Gig Werks Mailing List 

 Contact Us

 Connect

 Resources

  On Demand SharePoint Webcast Recordings
  Upcoming Webinars
  SharePoint Resources
  Business Intelligence Resources
  Gig Werks Website



©2009 Gig Werks. All rights reserved. Privacy Policy