SharePoint Data Retention Policy

Data retention policy enables you to apply a checkpoint on how long your data should be retained in the site. You can enforce compliance with legal and governmental regulations or internal business processes. As an administrator, you can set up a policy to control how to track documents, who has access to documents, and how long to retain document.

On theory part you can bing more. But here I will just take a short and quick tour on 

  • How to apply a policy
  • How we can automate this process

And the reason on why I am putting this in a blog where already so many details are available online. Simple reason when I need to work on this area in one of my project I had a tough time to gather appropriate information.

It was just scattered everywhere. So I decided to put the relevant piece here.

On that note lets look how we can apply a retention policy using sharepoint portal

Please follow the below steps to enable the Information management policy for a document library

Step 1: Data Retention Feature Activation.

Go to Site collection Settings => Site Collection Administrator=> Site Collection Feature and enable the following feature

Shortcut URL: /_layouts/15/ManageFeatures.aspx?Scope=Site

Feature Name

Feature Id

Locale

Library and Folder Based Retention

063c26fa-3ccc-4180-8a84-b6f98e991df3

English

 

   

  

Step 2: Define Site Collection Data retention Policy

Shortcut URL:   _layouts/15/Policylist.aspx

a. Goto Site collection Settings => Site Collection Administrator=> Content Type Policy Template.  

 

 b.  Click on Create and define the policy Name, Description and Statement.

 

 

 c. Click on Enable Retention

d. Click on Add retention stage and set the properties as applicable. Click on Ok on the Stage Properties dialogue.

 

e. Click Ok on the policy page

 

Step 3: Set the Data retention policy for the document library.

a. Navigate to the document library for which retention policy need to set and go to Library settings.

 b.Click on Information Management Policy Settings

 c. Click on the Content Type for which policy need to set

 

 

 d. Select either Define a policy and follow the steps mentioned at step 2.

 OR

Select the Use a Site collection Policy and select the policy from the dropdown created at Step 2.

 

 e. Click on Ok.

 

After completion of step 3 whenever the Information Management Policy and Expiration Policy Job are executed on the user site, documents will be deleted based on the defined policy.

 Now the most important part. Think for scenario where there are 250 site and we need to do all this juggling. Quite tough right? We have solution for that too. Using the piece of Powershell script we can automate the entire process.

1. First create a policy of your choice in a dev environemnt using the above mentioned steps.

2. Go to the policy details page. just click on the created policy. At the bottom of the page click on Export and you will get a policy xml.

3. Maintain a config file as given below. propertyId you can find out from the policy xml.

 <WebApplication name="SharePoint Portal">
<Sites>
<Site Title="Formulare">
<ManifestFile>policy.xml</ManifestFile>
<PolicyId>f0fc5d4d-06d4-454a-b19c-55f7d0aac1e3</PolicyId>
<ContentTypeId>CONTENTTYPE</ContentTypeId>
</Site>
</Sites>
</WebApplication>

4. Run the below script passing the config file name, it will add the policy for all the subsites of given web application for the items associated with the specified content type 

 function ApplyDataRetentionPolicy([String]$ConfigFileName = "")
{
  $configXml = [xml]$(get-content $configFileName -Encoding UTF8) 
    $webApplication = <WEBAPPURL> | Where-Object {$_.name -eq $configXml.Webapplication.name}
 if($webApplication -ne $null)
   {
       foreach ($site in $configxml.Webapplication.Sites.Site)
     {
           $siteConfiguration = $webApplication.Sites.Site | Where-Object {$_.Title -eq $site.Title}
           $PortalUrl = $siteConfiguration.Path
            $policyFeatureId=$site.PolicyId
         $manifestXml = get-content $site.ManifestFile -Encoding UTF8
            $DakContentType=$site.ContentTypeId

         $siteObj = Get-SPSite $PortalUrl            
            $activeFeature = $siteObj.Features["063c26fa-3ccc-4180-8a84-b6f98e991df3"]
          if ($activeFeature -eq $null)
           {
               $feature=Get-SPFeature -Identity 063c26fa-3ccc-4180-8a84-b6f98e991df3
               Enable-SPFeature -identity $feature.DisplayName -URL $PortalUrl
             Write-host "Feature activated on the site successfully" -ForegroundColor Green
          }
           else
            {
               Write-Host "Feature is alreday activated on the site" -ForegroundColor Yellow
           }

           $policyCatalog = new-object Microsoft.Office.RecordsManagement.InformationPolicy.PolicyCatalog($siteObj)
            $SiteCollpolicy = $policyCatalog.PolicyList[$policyFeatureId]
           try 
            {
               if($SiteCollpolicy -eq $null)
               {
                   [Microsoft.Office.RecordsManagement.InformationPolicy.PolicyCollection]::Add($siteObj, $manifestXml)
                    write-host "Created site collection policy" -ForegroundColor Green
              }
               else
                {
                   write-host "Policy already exist" -ForegroundColor Yellow
                   write-host "Delete existing policy" -ForegroundColor White
                  [Microsoft.Office.RecordsManagement.InformationPolicy.PolicyCollection]::Delete($siteObj,$policyFeatureId);
                 write-host "Creating New Policy" -ForegroundColor white
                 [Microsoft.Office.RecordsManagement.InformationPolicy.PolicyCollection]::Add($siteObj, $manifestXml)
                    write-host "Created site collection policy" -ForegroundColor Green
              }
           }
           catch 
          {
               write-host $_.Exception.Message
         }
           
                    try
                  {
                      Write-host "Bind policy to DAKFormulare" -ForegroundColor white
                     $ctype=$siteObj.RootWeb.ContentTypes[$DakContentType]
                       
                        [Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::CreatePolicy($ctype, $SiteCollpolicy);
                       Write-host "Complete policy creation for DAKFormulare" -ForegroundColor Green
                   }
                   catch 
                  {
                       write-host $_.Exception.Message -ForegroundColor Red
                    }
}
}
$siteObj.Dispose()
}