Compartilhar via


WorkItemTimerJobState Class

Used to track state for each iteration through processing work items.

Inheritance Hierarchy

System.Object
  Microsoft.Office.Server.Utilities.WorkItemTimerJobState

Namespace:  Microsoft.Office.Server.Utilities
Assembly:  Microsoft.Office.Server (in Microsoft.Office.Server.dll)

Syntax

'Declaration
Public Class WorkItemTimerJobState
'Usage
Dim instance As WorkItemTimerJobState
public class WorkItemTimerJobState

Remarks

Derive a class from this class in order to support executing code at the points where sites and site collections are changed while processing work items in a timer job that is derived from a SPWorkItemJobDefinition object.

The following example code demonstrates a sample work item job definition.

Examples

using System;
using System.Globalization;
using System.Threading;

using Microsoft.Office.Server.Utilities;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;


namespace Microsoft.SDK.SharePointServer.Samples
{
    /// <summary>
    /// This a sample work item timer job definition using TimerJobUtility
    /// </summary>
    /// <remarks>
    /// <para>
    /// This example shows the structure of a work item timer job and
    /// has comments for where you would add your code to perform specific
    /// tasks.
    /// </para>
    /// </remarks>
    public sealed class SampleWorkItemJobDefinition : SPWorkItemJobDefinition
    {
        // Replace with your job name.  Must be unique in the farm.
        private static string jobName = "AdventureWorksSampleWorkItemTimerJob";

        // Replace this with your own GUID
        private static Guid sampleWorkItemType = new Guid("3E445118-77B7-4917-A8E0-C4D729AC5805");

        /// <summary>
        /// Instance of the state class.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Keeps track of the state of the job across work items.
        /// </para>
        /// <para>
        /// This is instantiated and disposed within the Execute method since the instance
        /// of the SPWorkItemJobDefinition class can be reused by the timer job
        /// infrasturcture.
        /// </para>
        /// </remarks>
        private SampleWorkItemJobState timerJobState;

        /// <summary>
        /// This class keeps track of the state of the work item job.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Add properties to this class for any state you need kept.
        /// </para>
        /// </remarks>
        internal class SampleWorkItemJobState : WorkItemTimerJobState
        {
            // Set this to false if you do not require the WorkItemTimerJobState.Web
            // property to be fetched for each work item
            private static bool fetchWeb = true;

            // Set this to false if you do not require the WorkItemTimerJobState.User
            // property to be fetched for each work item
            private static bool fetchUser = true;

            /// <summary>
            /// Default constructor.
            /// </summary>
            public SampleWorkItemJobState()
                : base(SampleWorkItemJobState.fetchWeb, SampleWorkItemJobState.fetchUser)
            {
            }

            /// <summary>
            /// Called after changing the <see cref="Site" /> for next work item.
            /// </summary>
            /// <param name="wi"></param>
            protected override void OnSiteChanged(SPWorkItem workItem)
            {
                base.OnSiteChanged(workItem);

                // Add your code here if you need to update any state that is tied
                // to the SPSite of the work item
                // Similarly you can override OnSiteChanging to perform any per-SPSite
                // processing required before the SPSite is change.
                // Similarly you can override OnWebChanging/OnWebChanged for any state
                // or processing required on a per-SPWeb basis.
            }
        }

        /// <summary>
        /// Process one work item
        /// </summary>
        /// <param name="workItem">work item to process</param>
        /// <param name="timerJobState">job state</param>
        /// <remarks>
        /// <para>
        /// This method is called in the culture of timerJobState.Web
        /// </para>
        /// </remarks>
        private void ProcessWorkItemCore(SPWorkItem workItem, SampleWorkItemJobState timerJobState)
        {
            // Put your code here
        }


        /// <summary>
        /// Default constructor.  Only used for deserialization.
        /// </summary>
        public SampleWorkItemJobDefinition() : base()
        {
        }

        /// <summary>
        /// Public constructor.  Use this to register the job on a web application.
        /// </summary>
        /// <param name="webApplication">web application to register job to run on</param>
        /// <remarks>
        /// <para>
        /// This job will process all site collections within the web application.
        /// </para>
        /// <para>
        /// See the registration PowerShell script for how to register the job.
        /// </para>
        /// </remarks>
        public SampleWorkItemJobDefinition(SPWebApplication webApplication)
            : base(SampleWorkItemJobDefinition.jobName, webApplication)
        {
        }

        /// <summary>
        /// Name of the job
        /// </summary>
        /// <remarks>
        /// <para>
        /// This will be shown in the central administration timer job UI.
        /// </para>
        /// </remarks>
        public override string DisplayName
        {
            get
            {
                return Microsoft.SDK.SharePointServer.Samples.SampleWorkItemJob.JobDisplayName;
            }
        }

        /// <summary>
        /// Description of the job
        /// </summary>
        /// <remarks>
        /// <para>
        /// This will be shown in the central administration timer job UI.
        /// </para>
        /// </remarks>
        public override string Description
        {
            get
            {
                return Microsoft.SDK.SharePointServer.Samples.SampleWorkItemJob.JobDescription;
            }
        }

        /// <summary>
        /// Type of work item
        /// </summary>
        /// <returns>The Guid representing the type of work item</returns>
        public override Guid WorkItemType()
        {
            return SampleWorkItemJobDefinition.sampleWorkItemType;
        }

        public override void Execute(SPJobState jobState)
        {
            // create an instance of the state class
            timerJobState = new SampleWorkItemJobState();

            try
            {
                // Call the base class implementation which will process
                // the work items
                base.Execute(jobState);
            }
            finally
            {
                // destroy the job state instance to ensure it
                // does not get reused
                timerJobState.Dispose();
                timerJobState = null;
            }
        }

        protected override bool ProcessWorkItem(
            SPContentDatabase db,
            SPWorkItemCollection workItems,
            SPWorkItem workItem,
            SPJobState jobState)
        {
            TimerJobUtility tju = new TimerJobUtility(SampleWorkItemJobDefinition.jobName, jobState);

            // Call ProcessWorkItem, passing in our timerJobState
            return tju.ProcessWorkItem(workItems, workItem, this.timerJobState, 
                delegate(SPWorkItem wi, WorkItemTimerJobState tjs)
                {
                    // Save the current cultures
                    CultureInfo savedCulture = Thread.CurrentThread.CurrentCulture;
                    CultureInfo savedUICulture = Thread.CurrentThread.CurrentUICulture;

                    try
                    {
                        SampleWorkItemJobState sampleTJS = (SampleWorkItemJobState)tjs;

                        // Set the thread cultures to match the work item web
                        Thread.CurrentThread.CurrentCulture = sampleTJS.Web.Locale;
                        Thread.CurrentThread.CurrentUICulture = sampleTJS.Web.UICulture;

                        // Process the work item
                        ProcessWorkItemCore(wi, sampleTJS);
                    }
                    catch
                    {
                        // protect against exception filters running in the web's culture
                        throw;
                    }
                    finally
                    {
                        // Restore the thread cultures
                        Thread.CurrentThread.CurrentCulture = savedCulture;
                        Thread.CurrentThread.CurrentUICulture = savedUICulture;
                    }
                }
            );
        }
    }
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

WorkItemTimerJobState Members

Microsoft.Office.Server.Utilities Namespace