#SPSDC @PGBhoyar
Presented By: Prashant G Bhoyar
Building Solutions using
SharePoint Timer Jobs
08 June 2013
#SPSDC @PGBhoyar
Who am I?
#SPSDC @PGBhoyar
About today’s Session
• Raise your hand if something is not clear
• Sharing is Caring
• Ask Questions
• L...
#SPSDC @PGBhoyar
What Will We Cover Today?
• What are Timer Jobs?
• Common business scenarios for Timer Jobs
• Timer Job a...
#SPSDC @PGBhoyar
#SPSDC @PGBhoyar
What are Timer Jobs?
• Perform much of backend work to maintain farm
• Run on one or more server at sched...
#SPSDC @PGBhoyar
What are Timer Jobs?
• To Summarize,
#SPSDC @PGBhoyar
Examples of Timer Jobs in
SharePoint
• Clean up Old Sites
• User Profile Sync
• Solution Deployment
• Sea...
#SPSDC @PGBhoyar
#SPSDC @PGBhoyar
LessComplexity
Timer Jobs Vs Scheduled Tasks
Scheduled Tasks Timer Jobs
-Create Console App
-Schedule it ...
#SPSDC @PGBhoyar
Timer Jobs in SharePoint Farm
• Central Admin -> Monitoring ->Review Job Definitions
#SPSDC @PGBhoyar
Timer Jobs in SharePoint Farm
• Windows SharePoint Services Timer Service(SPTimerV4) run Timer Job
• Serv...
#SPSDC @PGBhoyar
#SPSDC @PGBhoyar
Create Custom Timer Jobs
• Visual Studio -> Empty SharePoint Project
• Deploy as Farm Solution
#SPSDC @PGBhoyar
LessComplexity
Architecture of Timer Jobs
• Microsoft.SharePoint.Administration.SPJobDefinition : Timer
J...
#SPSDC @PGBhoyar
LessComplexity
Architecture of Timer Jobs
• Parameters of SPJobDefinition Constructor
Name Description
Na...
#SPSDC @PGBhoyar
LessComplexity
Architecture of Timer Jobs
• SPJobLockType values
Value Description
None No locks. The tim...
#SPSDC @PGBhoyar
LessComplexity
Architecture of Timer Jobs
• Override the Execute method of the SPJobDefinition class and
...
#SPSDC @PGBhoyar
LessComplexity
Configuration Options for Custom
Timer Jobs
• OWSTimer.Exe.Config (Not Recommended)
• Need...
#SPSDC @PGBhoyar
#SPSDC @PGBhoyar
Typical Timer Job Life Cycle
#SPSDC @PGBhoyar
#SPSDC @PGBhoyar
LessComplexity
Two options:
• Declarative by using SharePoint Feature
• Pros: Easy to deploy and maintain...
#SPSDC @PGBhoyar
LessComplexity
SPSchedule Class Types
Deployment and Registration of
Custom Timer Jobs
Name Description
S...
#SPSDC @PGBhoyar
LessComplexity
Option 01 : Declarative by using SharePoint
Feature
• Add Feature
• Add code to register
T...
#SPSDC @PGBhoyar
LessComplexity
Option 02 : Programmatically using Server side object model
• Create Console Application/P...
#SPSDC @PGBhoyar
#SPSDC @PGBhoyar
LessComplexity
• Attach the debugger to “OWSTIMER.EXE”
• Debug -> Attach to Process
Debug Custom Timer Jo...
#SPSDC @PGBhoyar
LessComplexity
• Check the History To see when the timer job ran last time
Debug Custom Timer Jobs
#SPSDC @PGBhoyar
#SPSDC @PGBhoyar
LessComplexity
• Change the Schedule using Central Admin
• Change the Schedule using PowerShell Commands
...
#SPSDC @PGBhoyar
#SPSDC @PGBhoyar
LessComplexity
PowerShell Commands
• To retrieve all available timer jobs
• Get-SPTimerJob cmdlet
• To se...
#SPSDC @PGBhoyar
DEMO
#SPSDC @PGBhoyar
LessComplexity
Developing Custom Timer Jobs
• Set up the solution
• Add a class and Inherit from SPJobDef...
#SPSDC @PGBhoyar
#SPSDC @PGBhoyar
Common Issues and Fixes
• Redeployment : IISRESET and ReStart Timer Services
• Debugging takes lot of tim...
#SPSDC @PGBhoyar
Best Practices
• Always implement Exception Handling
• SPContext is NOT AVAILABLE. Never use it in Timer ...
#SPSDC @PGBhoyar
When not to use Timer Jobs?
• OOTB Options are available
• Content/Data needs to be updated synchronously...
#SPSDC @PGBhoyar
LessComplexity
Is Timer Job Right Choice?
• Yes if,
• OOTB options are not available
• Data update/Operat...
#SPSDC @PGBhoyar
References
Appendix/Resources
MSDN:
http://msdn.microsoft.com/en-us/library/hh528519(v=office.14).aspx
ht...
#SPSDC @PGBhoyar
Thanks to our sponsors!
#SPSDC @PGBhoyar
SharePint!!!
#SPSDC @PGBhoyar
Questions? Feedback? Contact me:
 Twitter: @PGBhoyar
 Blog: http://pgbhoyar.wordpress.com (limited cont...
Upcoming SlideShare
Loading in …5
×

SPSDC 2013 Building Solutions using SharePoint Timer Jobs

3,154 views

Published on

Slides of my session on SharePoint 2010 Timer Jobs at SPSDC 2013 in Room 5182A at 12:45 PM.
Location:
Microsoft Chevy Chase
5404 Wisconsin Ave
Chevy Chase, MD 20815

Published in: Education, Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,154
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
68
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

SPSDC 2013 Building Solutions using SharePoint Timer Jobs

  1. 1. #SPSDC @PGBhoyar Presented By: Prashant G Bhoyar Building Solutions using SharePoint Timer Jobs 08 June 2013
  2. 2. #SPSDC @PGBhoyar Who am I?
  3. 3. #SPSDC @PGBhoyar About today’s Session • Raise your hand if something is not clear • Sharing is Caring • Ask Questions • Let the learning begins…
  4. 4. #SPSDC @PGBhoyar What Will We Cover Today? • What are Timer Jobs? • Common business scenarios for Timer Jobs • Timer Job architecture • Developing Timer Jobs • Various approaches to registering Timer Jobs • How to Test/Debug Timer Jobs • Common issues and fixes for Timer Jobs • Timer Jobs best practices • When not to use them
  5. 5. #SPSDC @PGBhoyar
  6. 6. #SPSDC @PGBhoyar What are Timer Jobs? • Perform much of backend work to maintain farm • Run on one or more server at scheduled time • Run periodically and independent of the users • Offload long running processes from web front end server • Run code under higher privileges
  7. 7. #SPSDC @PGBhoyar What are Timer Jobs? • To Summarize,
  8. 8. #SPSDC @PGBhoyar Examples of Timer Jobs in SharePoint • Clean up Old Sites • User Profile Sync • Solution Deployment • Search Index • Various other Automations/Long Running operations
  9. 9. #SPSDC @PGBhoyar
  10. 10. #SPSDC @PGBhoyar LessComplexity Timer Jobs Vs Scheduled Tasks Scheduled Tasks Timer Jobs -Create Console App -Schedule it using Windows Tasks Scheduler -Easy to setup -Create Custom Timer Job -Register in SharePoint -No reporting available -Need to implement custom logging -Can track status, history, change the schedule, start/stop using Central Admin. -Need direct access to SharePoint Servers (Not easy to get) -Request special account to run the Scheduled Tasks -Timer Job runs under SharePoint Timer Job Account -Load Balancing is not available -Load balancing is available -Console Application -Full Access to SharePoint API (SPSite, SPWebApplication)
  11. 11. #SPSDC @PGBhoyar Timer Jobs in SharePoint Farm • Central Admin -> Monitoring ->Review Job Definitions
  12. 12. #SPSDC @PGBhoyar Timer Jobs in SharePoint Farm • Windows SharePoint Services Timer Service(SPTimerV4) run Timer Job • Service must be enabled and running in each server • Start –Administrative Tools -> Services • The timer job executes under OWSTIMER.exe
  13. 13. #SPSDC @PGBhoyar
  14. 14. #SPSDC @PGBhoyar Create Custom Timer Jobs • Visual Studio -> Empty SharePoint Project • Deploy as Farm Solution
  15. 15. #SPSDC @PGBhoyar LessComplexity Architecture of Timer Jobs • Microsoft.SharePoint.Administration.SPJobDefinition : Timer Jobs are created and executed by using this class • Three Constructors • Default (No Parameters): • For internal use • Others : • Job Name • Job Lock Type • Web Application or Service
  16. 16. #SPSDC @PGBhoyar LessComplexity Architecture of Timer Jobs • Parameters of SPJobDefinition Constructor Name Description Name Name of the Job Service An instance of the SPService class that owns this job. Only the servers where the service, represented by the SPService object, is running can run this job. WebApplication Parent WebApplication Server An instance of the SPServer class associated with this job. Pass null if this job is not associated with a specific server. lockType An SPJobLockType value that indicates the circumstances under which multiple instances of the job can be run simultaneously. http://msdn.microsoft.com/en-us/library/hh528519(v=office.14).aspx
  17. 17. #SPSDC @PGBhoyar LessComplexity Architecture of Timer Jobs • SPJobLockType values Value Description None No locks. The timer job runs on every machine on which the parent service is provisioned. ContentDatabase Job runs for each content database associated with the job's web application. Job Only one server can run the job at a time.
  18. 18. #SPSDC @PGBhoyar LessComplexity Architecture of Timer Jobs • Override the Execute method of the SPJobDefinition class and replace the code in that method with the code that your job requires. • The targetInstanceId maps to the Guid of the Current content database while the timer job is running
  19. 19. #SPSDC @PGBhoyar LessComplexity Configuration Options for Custom Timer Jobs • OWSTimer.Exe.Config (Not Recommended) • Need to modify manually in each server • External Files : (For Example config.xml) • SharePoint Lists: • SharePoint Object Property Bag
  20. 20. #SPSDC @PGBhoyar
  21. 21. #SPSDC @PGBhoyar Typical Timer Job Life Cycle
  22. 22. #SPSDC @PGBhoyar
  23. 23. #SPSDC @PGBhoyar LessComplexity Two options: • Declarative by using SharePoint Feature • Pros: Easy to deploy and maintain • Cons: Difficult to develop • Programmatically using Server side object model • Pros : Flexible, Easier to Develop • Cons : Difficult to deploy and maintain Deployment and Registration of Custom Timer Jobs
  24. 24. #SPSDC @PGBhoyar LessComplexity SPSchedule Class Types Deployment and Registration of Custom Timer Jobs Name Description SPMinuteSchedule Runs the job every x number of minutes. This class can be used to schedule jobs for periods of time other than hour, day, week, month, or year. For example, to run a job every 11 days, use this class and set its Interval property to 15840 minutes. SPHourlySchedule Runs the job every hour. SPDailySchedule Runs the job daily. SPWeeklySchedule Runs the job weekly. SPMonthlySchedule Runs the job monthly. SPYearlySchedule Runs the job yearly.
  25. 25. #SPSDC @PGBhoyar LessComplexity Option 01 : Declarative by using SharePoint Feature • Add Feature • Add code to register Timer Job in the FeatureActivated • Add code to delete Timer Job in the FeatureDeActivating Deployment and Registration of Custom Timer Jobs
  26. 26. #SPSDC @PGBhoyar LessComplexity Option 02 : Programmatically using Server side object model • Create Console Application/Power Shell Script • Add code to register Timer Job • Add code to delete Timer Job Deployment and Registration of Custom Timer Jobs
  27. 27. #SPSDC @PGBhoyar
  28. 28. #SPSDC @PGBhoyar LessComplexity • Attach the debugger to “OWSTIMER.EXE” • Debug -> Attach to Process Debug Custom Timer Jobs
  29. 29. #SPSDC @PGBhoyar LessComplexity • Check the History To see when the timer job ran last time Debug Custom Timer Jobs
  30. 30. #SPSDC @PGBhoyar
  31. 31. #SPSDC @PGBhoyar LessComplexity • Change the Schedule using Central Admin • Change the Schedule using PowerShell Commands • $timerJob = Get-SPTimerJob -Identity "SPSDC2013 TimerJob01" • Start-SPTimerJob $timerJob • Easiest : Write Console Application to debug business logic Expedite Debugging
  32. 32. #SPSDC @PGBhoyar
  33. 33. #SPSDC @PGBhoyar LessComplexity PowerShell Commands • To retrieve all available timer jobs • Get-SPTimerJob cmdlet • To set the schedule • $timerJob = Get-SPTimerJob -Identity "SPSDC2013 TimerJob01" • Set-SPTimerJob -Identity $timerJob -Schedule "Every 4 minutes between 0 and 59“ • To Start a Timer Job • $timerJob = Get-SPTimerJob -Identity " SPSDC2013 TimerJob01 " • Start-SPTimerJob $timerJob
  34. 34. #SPSDC @PGBhoyar DEMO
  35. 35. #SPSDC @PGBhoyar LessComplexity Developing Custom Timer Jobs • Set up the solution • Add a class and Inherit from SPJobDefinition • Override Execute Method • Write Business Logic • Register Timer Jobs
  36. 36. #SPSDC @PGBhoyar
  37. 37. #SPSDC @PGBhoyar Common Issues and Fixes • Redeployment : IISRESET and ReStart Timer Services • Debugging takes lot of time :Use Console Application to debug the business login during development • Always implement Exception Handling • SPContext is NOT AVAILABLE. Never use it in Timer Jobs. • If you use SiteCollection Feature to register timer job, activate/deactivate using PowerShell • If SPJobLockType is set to ContentDatabase, timer job will get fired multiple times depending on number of Content Databases
  38. 38. #SPSDC @PGBhoyar Best Practices • Always implement Exception Handling • SPContext is NOT AVAILABLE. Never use it in Timer Jobs. • Avoid using OSTTIMER.EXE.Config to store configuration entries • In production restart the Timer Services using command • “Get-SPTimerJob job-timer-recycle | Start-SPTimerJob” • Use Console Application to debug the business login during development
  39. 39. #SPSDC @PGBhoyar When not to use Timer Jobs? • OOTB Options are available • Content/Data needs to be updated synchronously • Simple data processing that can be easily handled with Event Receivers
  40. 40. #SPSDC @PGBhoyar LessComplexity Is Timer Job Right Choice? • Yes if, • OOTB options are not available • Data update/Operation can wait • Complex time-consuming processing logic • Would like to restrict certain activities at specific time
  41. 41. #SPSDC @PGBhoyar References Appendix/Resources MSDN: http://msdn.microsoft.com/en-us/library/hh528519(v=office.14).aspx http://technet.microsoft.com/en-us/library/cc678870(v=office.12).aspx http://www.andrewconnell.com/Creating-Custom-SharePoint-Timer-Jobs http://msdn.microsoft.com/en-us/library/cc406686.aspx
  42. 42. #SPSDC @PGBhoyar Thanks to our sponsors!
  43. 43. #SPSDC @PGBhoyar SharePint!!!
  44. 44. #SPSDC @PGBhoyar Questions? Feedback? Contact me:  Twitter: @PGBhoyar  Blog: http://pgbhoyar.wordpress.com (limited contents)  Email: pgbhoyar@gmail.com Thank You Organizers, Sponsors and You for Making this Possible.

×