SharePoint Events

 ProjectReady Applications for the AEC
 06/28/16 - Webcast: Compliant Legal Document Control & Collaboration in SharePoint
 06/30/16 - Webcast: AutoCAD & Document Management, Viewing, Markup & Mobility in SharePoint
 07/01/16 - Webcast: Best New Security Features of SharePoint 2016 and SharePoint Online
 07/26/16 - Webcast: Project & Program Management Made Easy in SharePoint & Office 365
 07/27/16 - Webcast: Matter Management Engine in SharePoint & Microsoft Outlook
 07/28/16 - Webcast: Project & Program Management for the AEC with SharePoint
 07/29/16 - Webcast: Why Office 365 and the Microsoft Cloud is Ready for the Financial Services Industry
(More Links...)

 SharePoint Videos

 Best New Features of SharePoint 2016
 The Value of the Microsoft Cloud: Understanding the Scalability, Affordable Storage & Security Advantages of Office 365
 Task, Resource & Utilization Management & Reporting for the AEC with SharePoint and Office 365
 Legal Matter Task Management and Reports in SharePoint and Office 365
 What’s Coming in Office 365 & SharePoint 2016
 Automating Contract Management with Workflow in the Cloud or On-Premise
 Manage Email in the Cloud or On-Premise with SharePoint & Office 365
 Governance Essentials for SharePoint 2013 and SharePoint Online
 Easily Move Email, Files and SharePoint to Office 365
 Enterprise Workflow in the cloud with SharePoint & Office 365
 Why SharePoint 2013
 SharePoint 2013 vs SharePoint Online and Office 365 Security, Compliance and eDiscovery
 Expanding the Use, Potential and Value of SharePoint and Office 365 through 3rd Party Applications
 Keys to Successful SharePoint Initiatives and User Adoption
 SharePoint 2013 vs. SharePoint Online: Workflow
 Ensure Auditability and Compliance with Advanced Workflows in SharePoint
 SharePoint 2013 vs. SharePoint Online and Office 365: Enterprise Content Management
 SharePoint 2013 vs. SharePoint Online and Office 365: Business Intelligence
 SharePoint 2013 vs. SharePoint Online: Search
 Power Business Intelligence for SharePoint Online and Office 365
(More Links...)

 Archives ‭[1]‬

Opening SharePoint Links in a new window
Mail Enabled Lists vs. The Missing Windows 2008 POP3/IMAP Server 
7 Tools for SharePoint Developers
Public Facing Masterpage Techniques
How to Quickly Deploy and Activate a Timer Service to Your Site Collection
Custom SharePoint Master Page Feature with WSP Builder
Date Math with InfoPath
Enterprise Search Tricks and Tips Part 1
Populating Word Documents With SharePoint Data. Try The DIP!
Programmatic Deep Dive into Blank SharePoint Lookup Columns
1 - 10Next
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

        

 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