SPSNJ 2013 Building Solutions using SharePoint TimerJobs

39
#SPSNJ @PGBhoyar Presented By: Prashant G Bhoyar Building Solutions using SharePoint Timer Jobs 05 October 2013

Transcript of SPSNJ 2013 Building Solutions using SharePoint TimerJobs

Page 1: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Presented By: Prashant G Bhoyar

Building Solutions using SharePoint Timer Jobs

05 October 2013

Page 2: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Who am I?

• SharePoint Consultant at Portal Solutions

• Product - AuthentiMate

• Services – We love SharePoint ..

• Guy with multiple hats

• University of Maryland College Park Alumni

• Recipient of Antarctic Service Medal

Page 3: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @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

Page 4: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Page 5: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @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

Page 6: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

What are Timer Jobs?• To Summarize,

Page 7: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Examples of Timer Jobs in SharePoint• Clean up Old Sites

• User Profile Sync

• Solution Deployment

• Search Index

• Various other Automations/Long Running operations

Page 8: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Page 9: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Less

Co

mp

lexi

ty

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)

Page 10: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Timer Jobs in SharePoint Farm• Central Admin -> Monitoring ->Review Job Definitions

Page 11: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @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

Page 12: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Page 13: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Create Custom Timer Jobs• Visual Studio -> Empty SharePoint Project

• Deploy as Farm Solution

Page 14: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Less

Co

mp

lexi

ty

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

Page 15: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Less

Co

mp

lexi

ty

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

WebApplication Parent WebApplication

Server An instance of the SPServer class associated with this job

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

Page 16: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Less

Co

mp

lexi

ty

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.

Page 17: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Less

Co

mp

lexi

ty

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

Page 18: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Page 19: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Typical Timer Job Life Cycle

Page 20: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Page 21: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Less

Co

mp

lexi

ty

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

Page 22: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Less

Co

mp

lexi

ty

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

Page 23: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Less

Co

mp

lexi

ty

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

Page 24: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Page 25: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Less

Co

mp

lexi

ty

• Attach the debugger to “OWSTIMER.EXE”

• Debug -> Attach to Process

Debug Custom Timer Jobs

Page 26: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Less

Co

mp

lexi

ty

• Check the History To see when the timer job ran last time

Debug Custom Timer Jobs

Page 27: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Page 28: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Less

Co

mp

lexi

ty

• Change the Schedule using Central Admin

• Change the Schedule using PowerShell Commands

• $timerJob = Get-SPTimerJob -Identity "SPSDC2013TimerJob01"

• Start-SPTimerJob $timerJob

• Easiest : Write Console Application to debug business logic

Expedite Debugging

Page 29: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

DEMO

Page 30: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Less

Co

mp

lexi

ty

Developing Custom Timer Jobs• Set up the solution

• Add a class and Inherit from SPJobDefinition

• Override Execute Method

• Register Timer Jobs

Page 31: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Page 32: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @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

Page 33: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Best Practices• Always implement Exception Handling

• SPContext is NOT AVAILABLE. Never use it in Timer Jobs.

• 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

Page 34: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @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

Page 35: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Outcome

Page 36: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

References

Appendix/ResourcesMSDN: http://msdn.microsoft.com/en-us/library/hh528519(v=office.14).aspxhttp://technet.microsoft.com/en-us/library/cc678870(v=office.12).aspxhttp://www.andrewconnell.com/Creating-Custom-SharePoint-Timer-Jobshttp://msdn.microsoft.com/en-us/library/cc406686.aspx

Page 37: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Princeton SharePoint user group• Different SharePoint

discussions each month on various topics. Announced on meetup.com

• Meets 4th Wednesday of every month

• 6pm – 8pm• Infragistics Office • 2 Commerce Drive, Cranbury,

NJ• http://www.meetup.com/prin

cetonSUG• http://www.princetonsug.com

Page 38: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Thank You Event Sponsors

• Platinum & Gold sponsors have tables here in the Fireside Lounge

• Please visit them and inquire about their products & services

• To be eligible for prizes make sure your bingo card is signed by all Platinum/Gold

Page 39: SPSNJ 2013 Building Solutions using SharePoint TimerJobs

#SPSNJ @PGBhoyar

Questions? Feedback? Contact me:

Twitter: @PGBhoyar Blog: http://pgbhoyar.wordpress.com (limited contents) Email: [email protected]

Thank YouOrganizers, Sponsors and You for Making this Possible.