Microsoft uses timer jobs to do things like dead web cleanup (purging unused sites from site collections among others.
Windows SharePoint Services 3.0 lets you create custom jobs that are executed at set intervals. These jobs, known as timer jobs, are similar to those tasks that you can create in any version of Windows by using the Task Scheduler application. This capability is useful for scheduled process jobs because you keep everything in Windows SharePoint Services rather than create a console .exe file that is configured to run at set intervals by using the Windows Task Scheduler.
You can create your own custom timer jobs to do your own scheduled tasks.
The major benefits to using the Windows SharePoint Services timer service compared to using Windows Task Scheduler:
Benefits:
a. Timer service knows the topology of the Office SharePoint Server farm, and that you can load balance the jobs across all the servers in the farm or tie them to specific servers that are running particular services
b. Once your timer job has been installed you can view it's status through Central Administration and even disable/enable it... all without console access to your production servers!
c. When your job runs, MOSS passes it the GUID of the content database for the site the job is registered with. You can use this GUID to obtain a reference to a content database, then a site collection, and finally a site within the collection (SPWeb)
d. No user involvement and it is easier to maintain a Timer Job. You can manage it directly from Central Administration.
e. Timer Jobs are created at the web application level.
Steps to create a Custom Timer Job:
Creating custom timer jobs in Windows SharePoint Services 3.0 can be useful when a project requires a scheduled process to be executed. Timer jobs are the preferred way to create these scheduled tasks instead of creating a console application scheduled with Windows Task Scheduler.
Timer jobs are executed by using the Windows SharePoint Services Timer service (Owstimer.exe).
1. The first thing you need to do is create a class that inherits from the Microsoft.SharePoint.Administration.SPJobDefinition class.
2. Depending upon the schedule that you want to set, you need to choose the classes.
For example we have SPWeeklySchedule, SPDailySchedule, SPMonthlySchedule, SPYearlySchedule, SPHourlySchedule, SPMinuteSchedule.
3. The SPJobDefinition class contains three overridden constructors.
a. Constructor that has no parameters is reserved for internal use.
b. The other two constructors set the timer job's name, the parent Web application or service (such as the search indexer), the server, and the job lock type
4. SPJobDefinition constructor (or constructors) which accept an SPJobLockType value. This parameter controls the locking behaviour of the Timer Job and can be one of the following values;
· ContentDatabase – Locks the content database. A timer job runs one time for each content database that is associated with the Web Application; therefore your job will run as many times for each content database associated with the Web Application that exists
· Job – Locks the timer job. Ensures that the job will be run only on a single server. i.e. it prevents multiple instances of the job from running on a single server (Recommended).
· None – No locks. The timer job runs on every machine on which the parent service is provisioned.
Based on these options you must decide which value is correct for your tasks, but, for the most part the SPJobLockType.Job option is probably the most likely option to use.
5. Override the Execute() virtual method (method for a "Windows SharePoint Services Timer" call when the job is executed.) . It receives a single parameter, the ID of the content database that is being processed by the job. When the timer job is registered or deployed, it is associated with a particular Web application. This is why the Execute method can assume that the WebApplication property is not null
Configuration Options
Many applications require some type of external configuration information. Configuration information could include things like the URLs to Web services or database connection information
One option is to create a configuration file for the SharePoint Timer service executable such as Owstimer.exe.config. This is not recommended for several reasons. First, you would have to modify the configuration file on each server in the farm. Second, you would have to make this change from the console instead of using the administration interface or through Windows SharePoint Services solution files (*.WSPs).
You have several other options when a custom timer job requires some type of external configuration data. These options include using the property bag on various Windows SharePoint Services objects, reading and parsing external files (such as a special XML configuration file), storing configuration data within SharePoint lists, or using the hierarchical storage.
External Files
a. Using external files for configuration data is very similar to creating an XML-based Web.config or *.exe.config file in common ASP.NET 2.0 applications and applications that are based on the Microsoft .NET Framework. The primary difference is that you must write the code to consume the data within the file. One approach is to create a class that is serializable to make reading and writing to the configuration file as simple as possible.
With the data stored in an external file, it can be managed by any application written to read and write to the file, or by an administrator who has console access to the server.
SharePoint Object Property Bag
In Windows SharePoint Services 3.0, Microsoft added a generic collection of properties to the following commonly used Windows SharePoint Services objects:
The SPWeb.Properties and SPListItem.Properties properties return an object of type Microsoft.SharePoint.Utilities.SPPropertyBag, which overrides theSystem.Collections.Specialized.StringDictionary object. The only difference between the two is that the SPPropertyBag object adds an Update method that commits changes to the property bag to the appropriate Windows SharePoint Services content database. Whenever you make changes to the property bag, you must call the Update method to save all the changes.
Using this technique, you can create application pages, site pages, Web Parts, or anything that can write to the current site's property bag.
SharePoint Lists
A third option available to you is to use SharePoint lists. SharePoint lists are appropriate to use when the timer job is associated with a specific site collection. You can create a special list (or lists) in the top level site of the site collection that the timer job interacts with. Working with SharePoint lists in timer jobs is no different from doing the same thing in a Web Part.
Hierarchical Object Storage
The WSS SDK mentions a new addition to Windows SharePoint Services that enables you to create and interact with administrative data by using a common framework to interact with the hierarchical object store. This storage construct lets you keep the configuration data for timer jobs in Windows SharePoint Services, instead of in some external file and not tied so closely with a particular SharePoint site as the list or property bag options do.
To add items to the object store, first create a class that will contain the data for the data to store. This class must inherit from theMicrosoft.SharePoint.Administration.SPPersistedObject object and must be serializable. To be serializable, it must have a default constructor that takes zero parameters. All properties that should be persisted must be implemented as public fields and decorated with theMicrosoft.SharePoint.Administration.PersistedAttribute attribute. The following class is used in the SharePointWarmupJob timer job.
Refer:
Following limitations in SharePoint stops accessing the configuration files in timer job.
· SharePoint timer job is running in different process named OWSTIMER.EXE.
- SharePoint web application or site will run with the help of process W3WP.EXE.
- So, the application configuration file is associated with the W3WP.EXE, so there is no way to access the web.config file in the owstimer.exe process at all. The context is completely different and out of domain. So, we need to call or access the web.config file explicitly in timer job.
- Usually the timer job installed as a feature. And all the logic of the timer job will go inside the Execute() method. So, write below statements in that method and access the complete web.config file.
If you want to access the web application or web site configuration [web.config] file in the SharePoint timer job.WebConfigurationManager class exposes a method OpenWebConfiguration to make it easier to read
Configuration config = WebConfigurationManager.OpenWebConfiguration("/", this.WebApplication.Name);
string _sqlConnectionString = config.ConnectionStrings.ConnectionStrings["DBConnectionString"].ToString();
OR
SPWebApplication webApplication = this.Parent as SPWebApplication;
Configuration config = WebConfigurationManager.OpenWebConfiguration("/", webApplication.Name);
String appValue = config.AppSettings.Settings["appSettingKey"].Value;
WebConfigurationManager is the class which is in the namespace Systen.Web.Configuration.
OpenWebConfiguration method uses a relative site URL within the IIS site, and the name of the site as it appears in IIS to load the configuration file. By default with SharePoint this will always match up the the SPWebApplication’s name property,
Deploying Custom Timer Jobs:
· The Custom Timer Job class has to be compiled into a signed assembly to generate a strong name.
· You must install this assembly into the GAC. With the assembly deployed to the GAC, you can now deploy, or install, the timer job to the Windows SharePoint Services farm.
· The WSS administrative interface does not provide a way to install the timer job
The "recommended way" for doing this would be to create a Feature Receiver and implement the FeatureActivated event. In this event, you can instantiate your job, set the job schedule and update the Job.
Uninstalling the timer job is handled by using the FeatureDeactivated method within the Feature receiver.
· Activating the feature will deploy this Timer Job on the web application.
Once the solution is deployed, you can either activate the feature using
stsadm -o activatefeature command or go to Central Administration -> Application Management -> Manage Web Application Features -> select your web application -> Activate your Feature
NOTE:
1. Timer job will not appear on the Timer Job Status page until it has been executed at least once
2. When you are using site-scoped or site collection–scoped Features to install the timer jobs: The account that does the scheduling must have write permissions to the configuration database of the server farm. Usually, the identity of the application pool that is hosting the SharePoint site does not have permissions on the configuration database. This is the account that is used when you activate the Feature through the browser interface. However, if you activate the Feature by using STSADM, the identity of the interactive user is used. This enables someone with greater user rights to install the timer job.
3. The Feature that handles the installation and uninstallation of a timer job should be a hidden Feature so that activation is only possible by using Stsadm.exe through the console. This is because when Features are activated through the Web interface, the application pool's identity is used to execute the code in the Feature receiver. This account typically does not have permissions to install the timer job. Instead, use an account that is part of the farm administrators group to activate the Feature using Stsadm.exe.
Troubleshooting:
1. If you make any changes to the timer job after it is deployed , and if these changes does not get reflected, Restart the Windows Sharepoint Services Timer service by typing the services.msc in the command prompt
Click on the link below to download a sample timer job. This job retrieves the contacts from a SharePoint list and updates to an external SQl database table. let me know for any queries
Sample Timer Job
Click on the link below to download a sample timer job. This job retrieves the contacts from a SharePoint list and updates to an external SQl database table. let me know for any queries
Sample Timer Job
No comments:
Post a Comment