Introduction
In this article you will see how to get the retention stage Id of the specified item using SharePoint Object Model. I have a custom list in which I have set the retention policy so that workflows must be initiated for the items based on the expiration formula. I have associated the following workflows in the custom list as shown in Figure 1.
Figure 1: Workflows
The custom list contains the following fields as shown in Figure 2:
Figure 2: Columns in the custom list
Retention Policy
I have added the following stages in the retention policy for the item content type as shown in Figure 3:
Figure 3: Retention Policy
[Note: Navigate to the custom list, click on "List settings" in the ribbon interface. Click on "Information management policy settings" under "Permissions and Management". Click on Item content type to set the retention policy.]
I have created an item in the custom list as shown in Figure 4.
Figure 4: Item in the custom list
Click on "Compliance Details" in the item ECB menu. You can see the expiration date set for the first stage as shown in Figure 5.
Figure 5: Compliance Details
The following procedure is needed to get the retention stage Id of the specified item using the SharePoint Object Model.
Use the following procedure to create a Console Application:
- Open "Visual Studio 2010" by going to "Start" | "All Programs" | "Microsoft Visual Studio 2010" then right-click on "Microsoft Visual Studio 2010" and click on "Run as administrator".
- Go to the "File" tab, click on "New" and then click on "Project".
- In the "New Project" dialog box, expand the "Visual C#" node, and then select the "Windows" node.
- In the "Templates" pane, select "Console Application".
- Enter the Name as "RetentionStageId" and then click "OK".
- In the Solution Explorer, right-click on the solution and then click on "Properties".
- Select the "Application" tab; check whether ".Net Framework 3.5" is selected for the Target Framework.
- Select the "Build" tab; check whether "Any CPU" is selected for the Platform Target.
- In the Solution Explorer, right-click on the "References" folder and click on "Add Reference".
- Add the following references:
1. Microsoft.SharePoint.dll
2. Microsoft.Office.Policy.dll
Program.cs
- Replace the code in Program.cs with the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.RecordsManagement.PolicyFeatures;
namespace RetentionStageId
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("https://serverName/sites/Vijai/"))
{
using (SPWeb web = site.OpenWeb())
{
//// Get the list
SPList list = web.Lists.TryGetList("Custom");
//// Check if the list exists
if (list != null)
{
//// Get the get item by id
SPListItem item = list.GetItemById(2);
//// Gets the next retention stage ID of the specified item.
Console.WriteLine("Retention stage ID: " + Expiration.GetItemRetentionStage(item));
Console.ReadLine();
}
}
}
}
}
}
- Hit F5.
Scenario 1
Value will be empty because none of the stages are completed. You will get the following output:
Figure 6: Output
Scenario 2
Stage 1 is completed and the expiration date is set for the second stage as shown in Figure 7.
Figure 7: Compliance Details
\Since stage 1 is completed you will get the output as 1 (which is the retention stage Id).
Figure 8: Output
Scenario 3
Stage 2 is completed for the second stage as shown in Figure 9.
Figure 9: Compliance Details
Since Stage 2 is completed you will get the output as 2 (which is the retention stage Id).
Figure 10: Output
Summary
Thus in this article you have seen how to get the retention stage Id of the specified item using the SharePoint Object Model.