Delete all list Item and file versions from site and sub sites using CSOM and PowerShell
The versions property is not available from client object model on the ListItem class as with server object model. I landed on the article SharePoint – Get List Item Versions Using Client Object Model that describes how to get a list item versions property using the method GetFileByServerRelativeUrl by passing the FileUrl property. The trick is to build the list item url as “sites/test/Lists/MyList/30_.000” where 30 is the item id for which the version history needs to be retrieved. Using that information I created a PowerShell to loop through all lists in the site collection and sub sites to delete all version history from lists.
The script below targets a SharePoint tenant environment.
Please note that I have used script Load-CSOMProperties.ps1 from blog post Loading Specific Values Using Lambda Expressions and the SharePoint CSOM API with Windows PowerShell to help with querying object properties like Lambda expressions in C#. The lines below demonstrate use of the Load-CSOMProperties.ps1 file which I copied in the same directory where the script DeleteAllVersionsFromListItems.ps1 is.
- Load the Load-CSOMProperties.ps1 file
Set-Location $PSScriptRoot
$pLoadCSOMProperties=(get-location).ToString()+”\Load-CSOMProperties.ps1″
. $pLoadCSOMProperties
- Retrieve ServerRelativeURL property from file object
Load-CSOMProperties -object $file -propertyNames @(“ServerRelativeUrl”);
You can download the script from technet.
from reshmeeauckloo http://bit.ly/2ozAHFb
|