Programmatically Get the Retention Stage Id of the Specified Item in SharePoint 2010

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.

  sharepoint1.png

Figure 1: Workflows

The custom list contains the following fields as shown in Figure 2:

  sharepoint2.png

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:

 sharepoint3.png

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.

  sharepoint4.png

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.

  sharepoint5.png

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:

  1. 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".
  2. Go to the "File" tab, click on "New" and then click on "Project".
  3. In the "New Project" dialog box, expand the "Visual C#" node, and then select the "Windows" node.
  4. In the "Templates" pane, select "Console Application".
  5. Enter the Name as "RetentionStageId" and then click "OK".
  6. In the Solution Explorer, right-click on the solution and then click on "Properties".
  7. Select the "Application" tab; check whether ".Net Framework 3.5" is selected for the Target Framework.
  8. Select the "Build" tab; check whether "Any CPU" is selected for the Platform Target.
  9. In the Solution Explorer, right-click on the "References" folder and click on "Add Reference".
  10. Add the following references:

1. Microsoft.SharePoint.dll
2.
 Microsoft.Office.Policy.dll

Program.cs

  1. 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();
                    }
                }
            }
        }
    }
}

  1. Hit F5.

Scenario 1

Value will be empty because none of the stages are completed. You will get the following output:

sharepoint6.png

Figure 6: Output
 
Scenario 2

Stage 1 is completed and the expiration date is set for the second stage as shown in Figure 7.

sharepoint7.png

Figure 7: Compliance Details

\Since stage 1 is completed you will get the output as 1 (which is the retention stage Id).

sharepoint8.png

Figure 8: Output

Scenario 3

Stage 2 is completed for the second stage as shown in Figure 9.

sharepoint9.png

Figure 9: Compliance Details

Since Stage 2 is completed you will get the output as 2 (which is the retention stage Id).

sharepoint10.png 
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.

Up Next
    Ebook Download
    View all
    Learn
    View all