Requirement: Archive completed expense forms to yearly archive libraries, declare them as records and destroy them 7 years after creation. This of course needs to be done without user intervention.
I set out to learn how to programmatically do this thru a monthly custom Timer job.
1. 2 Namespaces to call out for this solution:
- Microsoft.Office.RecordsManagement.InformationPolicy
- Microsoft.Office.RecordsManagement.RecordsRepository
Note: Records Management settings are inherited from the site collection settings which in our case allowed for manual record declaration.
2. Create a custom list policy by creating an XML string defining the first retention stage with a Delete action after “Created date + 7 years“:
Dim retentionXML As String = _
“<Schedules nextStageId=’2′>” + _
“<Schedule type=’Default’>” + _
“<stages>” + _
“<data stageId=’1‘>” + _
“<formula id=’Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn‘>” + _
“<number>7</number>” + _
“<property>Created</property>” + _
“<propertyId>8c06beca-0777-48f7-91c7-6da68bc07b69</propertyId>” + _
“<period>years</period>” + _
“</formula>” + _
“<action type=’action’ id=’Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.Delete‘ />” + _
“</data>” + _
“</stages>” + _
“</Schedule>” + _
“</Schedules>”
3. Create a method to both create and set the custom policy on the library passing in the retentionXML created above and the library where the archived forms are housed:
Private Sub CreateCustomPolicyOnlibrary(ByVal retentionXML as string, ByVal archiveLibrary as SPList)
Dim archivePolicySettings as Microsoft.Office.RecordsManagement.InformationPolicy.ListPolicySettings = New Microsoft.Office.RecordsManagement.InformationPolicy.ListPolicySettings(archiveLibrary)
If Not archivePolicySettings.ListHasPolicy Then
archivePolicySettings.UseListPolicy = True
archivePolicySettings.SetRetentionSchedule(retentionXML, “Policy for Archived Expense Forms.”)
archivePolicySettings.Update()
End If
End Sub
4. Declare each form a record:
Records.DeclareItemAsRecord(item) where item is the form (SPListItem)
Note: we had business logic that we had to do with the form prior to declaring it a record in the library. If you don’t then you can set record declaration to occur automatically when the form is added to the library by
Records.ConfigureListForAutoDeclaration(archiveLibrary, True)
5. Now that the policy is set on the library, the forms will be deleted after 7 years. This is done via 2 SharePoint Timer jobs:
- Information Management Policy Job – looks at all policies defined across the site collection and determines what action needs to be taken (in our case deleted if created date + 7 years)
- Expiration Policy Job – acts on the policy if required (in our case this is what actually deletes the form)
Mission accomplished! I hope you find this post useful.
Very useful indeed! Saved my day ! Thank you very much