How to force Expiration/Retention policy to execute on SharePoint 2010

The Expiration/Retention policy (part of Information Management Policy) is executed by a couple of timer jobs. In 2007 these jobs are under the PolicyConfigService at the Farm level, and you can modify the schedule of these jobs to run very frequently for debugging purposes (e.g. http://blah.winsmarts.com/2008-10-Authoring_custom_expiration_policies_and_actions_in_SharePoint_2007.aspx – you will need to search for the PolicyConfigService by name though, as the ID will be different for each farm).

In 2010 these timer jobs are no longer at the Farm level, but instead at the WebApplication level. You can force these to run using the following code:

using (SPSite site = new SPSite("http://sharePointServer"))
			{
				List<string> jobNamesToRun = new List<string>()
													{
														"Information management policy",
														"Expiration policy"
				                                   	};

				foreach (var name in jobNamesToRun)
				{
					var job = site.WebApplication.JobDefinitions.FirstOrDefault(
						j => j.DisplayName.Equals(name, StringComparison.InvariantCultureIgnoreCase));

					if (job != null)
					{
						Console.WriteLine("Running job '{0}'", name);
						job.RunNow();
					}
				}
			}
Console.WriteLine("Enter..");
Console.ReadLine();

It is important to execute the “Information management policy” job before the “Expiration policy” job. The first evaluates and set the expiration date for list items. The second is the job that actually executes the defined expiration action (e.g. run a workflow, etc).

Advertisement

About Bernado

Based in Australia, I am a freelance SharePoint and Dynamics CRM developer. I love developing innovative solutions that address business and everyday problems. Feel free to contact me if you think I can help you with your SharePoint or CRM implementation.
This entry was posted in Information Management Policy, SharePoint 2010, Timer Job. Bookmark the permalink.

4 Responses to How to force Expiration/Retention policy to execute on SharePoint 2010

  1. vnaresh says:

    What is the namespace for List class

    • Bernado says:

      Hi vnaresh,

      It should be the generic List<string> class. I have edited the post. The generic List<string> class is in the System.Collections.Generic namespace.

  2. Jason M Vale says:

    This only actually forces the Policy timer jobs to run, it doesn’t actually force the Policy itself to execute unless it has actually expired. Is there a way to force a Policy to a time period of less than 1 day for testing purposes? Thanks

    • Bernado says:

      Hey Jason,

      The first timer job, “Information management policy”, forces the policy’s formula to evaluate to determine if the items have expired. Therefore, this first job is the one that marks the items as expired (for those items that the formula evaluated to be expired).

      The second timer job, “Expiration policy”, kicks off the policy’s expiration action (e.g. move to recycle bin) for those items marked as expired by the first timer job.

      It might help to run the first timer job, then wait for a few minutes before running the second timer job

      Alternatively, you can change the frequency that the jobs run from Central Admin, or by code. The code below (of the top of my head) sets the job to run every 5 minutes.

      var timerJob = GetTimerJob();

      SPMinuteSchedule schedule = new SPMinuteSchedule();
      schedule.BeginSecond = 0;
      schedule.EndSecond = 59;
      schedule.Interval = 5;

      timerJob.Schedule = schedule;
      timerJob.Update();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s