Sharepoint 2016 : Enforcing Retention policy on OneDrive Documents.

You may be familiar with the Information Management Policies that can be used to configure the retention policy on documents, eg: Introduction to information management policies and Create and apply information management policies . However have you ever wondered who the document retention policies can be applied for the documents within the users's Mysites ? Sharepoint 2016 comes with a new feature called Compliance center and this feature will help us configuring such policies based on the site template used ! Wondering how ? Lets looks at the following steps..

 

Pre-requisites : Configure User profile service application : https://technet.microsoft.com/en-us/library/ee721052(v=office.16).aspx

Configure Mysites : https://technet.microsoft.com/en-us/library/ee624362(v=office.16).aspx

Configure Search Service application : https://technet.microsoft.com/en-in/library/gg502597(v=office.16).aspx

In my test case , the Mysite web app and Mysite host URL is : https://sp:28309/

 

Now, lets start the configuration.

 

  1. Create Compliance Policy Center :

You will need to create a site collection on the same web application as the MysiteHost. The Site template should be selected as "Compliance Policy Center" (Under enterprise templates) . You can use UI method or PowerShell commands to create the site.

In My case, the Compliance policy center URL is https://sp:28309/sites/CompliancePolicyCenter


     2. Set the property PolicyCenterUrl for the Mysite web application

     $webApp=Get-SPwebApplication https://sp:28309
    $webApp.Properties["PolicyCenterUrl"]="https://sp:28309/sites/CompliancePolicyCenter"
    $webApp.update()
    $webApp.Properties

     3. Access the Compliance Policy center site collection and create a new Deletion policy
     4. Create a new Rule
Provide the policy Name and then create a new Rule - You can choose the option to delete the files permanently or move them to recycble and options are available to rule for "created date" and "Modified date" , number of days etc.

Now the Policy and rule will look like below

 

     5. Assign the Compliance policy to a site template
Once the template is selected click on "Manage Assigned Policies" and select the policy.

You will see it as below - Make sure that you select "Mark Policy as Mandatory"

     6. Confirm that the policy is applied using the following URL from the site collection level
https://sp:28309/personal/contoso_administrator/_layouts/15/retentionpolicyCollectionAdminsettings.aspx

https://sp:28309/personal/contoso_administrator/_layouts/15/retentionpolicyadminsettings.aspx

Nice..We are done with the basic setup.. :-)

Now, I have a OneDriveForBusiness site for the user Contoso\spfarm and a document is created / uploaded which is now 8 days old. We will proceed with removing this file from the document library using the policy we created and set. Document : Upgrade-20171022-081946-944.log

     7. Crawl the Document
This document should be crawled and indexed by the associated search service application. If this item is NOT crawled or indexing failed for some reason , fix it. You can confirm it by issuing a search for the document

      8. GET and SET ip_lastcompliancepolicyprocesstime property
Now confirm that The ip_lastcompliancepolicyprocesstime property for the mysite web should be atleast 7 days behind from current day

 To check the property value
    $web=Get-spweb https://sp:28309/personal/contoso_spfarm/
    $web.AllProperties["ip_lastcompliancepolicyprocesstime”]
 To reset the value to an older date: (Execute this only if the value shows within 7 days from today)   
    $web=Get-spweb https://sp:28309/personal/contoso_spfarm/
    $ResetDate=(Get-date).AddDays(-10)
    $modifieddate=(Get-date $ResetDate -Format "MM/dd/yyyy hh:mm:ss tt")
    $Updatedate= (Get-Date $modifieddate).ToString("MM/dd/yyyy hh:mm:ss tt")
    $web.AllProperties["ip_lastcompliancepolicyprocesstime”]=$Updatedate
    $web.update()
    $web.AllProperties["ip_lastcompliancepolicyprocesstime”]

      9. Execute the Timer jobs
You can now execute the following Timer jobs in the same order mentioned - Make sure that you pick the right web application

When the Compliance Dar Task house keeping job is executed , you can see a task getting created in the "Dar Tasks" list in the compliance center site.

Executing the Compliance Dar processing job will create a few more items

Execute the Compliance Policy processing job , you may not see a difference in the number of items in the "Dar Tasks" lists , but a few updates like Task completion time will be updated.

     10. Done .. ! Test it now
Now Access the OneDrive for business for contoso_spfarm and the item is completely removed.

Critical Points to remember -

1. ALL the web applications in the farm should have a property PolicyCenterUrl set with the right URL - NOT just the Mysitehost web app2. The contents of the Mysite web application should be successfully crawled and indexed3. The ip_lastcompliancepolicyprocesstime property for each OneDriveforBusiness site should be atleast 7 days behind from current day4. The Timer jobs “Compliance Dar Tasks housekeeping”, “Compliance Dar processing” and “Compliance Policy processing” should be executed in the same order.

 

A sample PowerShell script is shared herewith to automate step 2,3 and 4. Make sure that you have the right values for $MysiteWebApp and $PolicySiteCollection variables.

# Code Disclaimer:
# Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment. THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and distribute the object code form of the Sample Code, provided that. You agree: (i) to not use Our name, logo, or trademarks to market Your software product in which the Sample Code is embedded; (ii) to include a valid copyright notice on Your software product in which the Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or lawsuits, including attorneys’ fees, that arise or result from the use or distribution of the Sample Code.
#*=============================================

 $MysiteWebApp ="https://sp:28309"
$PolicySiteCollection="https://sp:28309/sites/CompliancePolicyCenter"

$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null)
{
Write-Host "Loading SharePoint Powershell Snapin" -ForegroundColor Green
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}

$WebApp=get-spwebapplication $MysiteWebApp
$sites = $WebApp.sites
$Policyweb=get-spweb $PolicySiteCollection
$Darlist=$policyweb.lists["Dar Tasks"]

Write-host "Running History cleanup.." -ForegroundColor Yellow

foreach($item in $Darlist.Items)
{
$Darlist.getitembyid($item.id).delete()
}
Write-host "Finished History cleanup.." -ForegroundColor Green
write-host " "

Write-host "Setting the Web property value.. This may take more time depending on the number of mysites you have. " -ForegroundColor Yellow
write-host "****************************************"
foreach ($site in $sites)
{
$web=Get-spweb $site.url

if ($site.url -match "/personal/")
{
$ResetDate=(Get-date).AddDays(-10)
$modifieddate=(Get-date $ResetDate -Format "MM/dd/yyyy hh:mm:ss tt")
$Updatedate= (Get-Date $modifieddate).ToString("MM/dd/yyyy hh:mm:ss tt")
$web.AllProperties["ip_lastcompliancepolicyprocesstime”]=$Updatedate
write-host "Web URL : " $site.url -foregroundcolor magenta
write-host "Property value : " $web.AllProperties["ip_lastcompliancepolicyprocesstime”]-foregroundcolor magenta
write-host " "
$web.Update()
}
}

write-host "****************************************"
Write-host "Setting the Web property value completed. " -ForegroundColor Green
write-host " "

Write-host "Executing the Dar Tasks House Keeping Job" -ForegroundColor Yellow
$HouseKeepingJob=Get-SPTimerJob | where {$_.typename -eq "Microsoft.Office.CompliancePolicy.SharePoint.Internal.DarTaskHouseKeepingJobDefinition" -and $_.webapplication -match "Mysite"}
$HouseKeepingJob.RunNow()
sleep 60
write-host "Completed..." -ForegroundColor Green
write-host " "

Write-host "Executing the Dar Processing Job" -ForegroundColor Yellow
$DarprocessingJob=Get-SPTimerJob | where {$_.typename -eq "Microsoft.Office.CompliancePolicy.SharePoint.Internal.DarProcessingJobDefinition" -and $_.webapplication -match "Mysite"}
$DarprocessingJob.runNow()
Sleep 60
write-host "Completed..." -ForegroundColor Green
write-host " "

Write-host "Executing the Compliance Policy Processing Job" -ForegroundColor Yellow
$ComplianPolicyProcessingJob=Get-SPTimerJob | where {$_.typename -eq "Microsoft.Office.CompliancePolicy.SharePoint.Internal.PolicyProcessingJobDefinition" -and $_.webapplication -match "Mysite"}
$ComplianPolicyProcessingJob.RunNow()
Sleep 60
write-host "Completed..." -ForegroundColor Green

POST BY : Manjesh Menon [MSFT]