Home

PowerShell script to configure the List versionning before execute the migration to SP Online

Frequent Contributor

When you are in a migration project from SharePoint On Premise to SharePoint Online, the worst case is the SPList in which is the User decided to implement versionning without any limitation.

You can have thousand of versions to migrate into only one list which will require many hours to migrate, generally without any added value for the Users because they never realized that setting was set.

The Migration does not like that because it will be so long to execute that site migration.

In my case, i have a site in which the size used by with only 2 items (execl file less than 1MB) of one list was more than 400MB.

image_thumb_14A9D783.png

 

So that first will delete all the versions before the XX last ones:

function Delete-Version-History([string]$SPWebURL, [string]$SPListName, [int]$MaintainVersionNumber)
{
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null

    Write-Host " -------------------------------------------------------- "
    Write-Host "SPWeb URL to configure:", $SPWebURL  -foregroundcolor Red
    $site = new-object Microsoft.SharePoint.SPSite($SPWebURL)
    $web = $site.openweb()
    Write-Host "    >> SPWeb URL from Object:", $web.URL -foregroundcolor Green

    $SPlist = $web.Lists[$SPListName]
    Write-Host "    >> SPList to Configure:", $SPlist.Title -foregroundcolor Green
    Write-Host "    >> Total Item to check:", $SPlist.Items.Count -foregroundcolor Green

    foreach ($SPitem in $SPlist.Items)
    {
        Write-Host "       >>>>>> File Item:", $SPitem.Name, "- Version Total:", $SPItem.Versions.count -foregroundcolor Magenta
        $currentVersionsCount = $SPItem.Versions.count
        if($currentVersionsCount -gt $MaintainVersionNumber)
        {
            for($i=$currentVersionsCount-1; $i -gt $MaintainVersionNumber; $i--)
            {
                $SPItem.versions[$i].delete()
                Write-Host "             >>>>>> Version:", $i, " Deleted"

                $SPlist.Update();
            }
        }
    }

$web.Dispose();
$site.Dispose();

}
 
cls
Delete-Version-History
http://MyWebApplication/sites/MySPSite/MySPWeb "My List Name" 50

 

This case is cool, but sometime, it could be better to directly apply the Versionning and force the list to apply this settings immediatly, so that other script will be used to:

function Configure-Versionning-SPList([string]$SPWebURL, [string]$SPListName, [int]$MaintainMajorVersionNumber, [bool]$EnableMinorVersionYesNo, [int]$MaintainMinorVersionNumber)
{
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null

    Write-Host " -------------------------------------------------------- "
    Write-Host "SPWeb URL to configure:", $SPWebURL  -foregroundcolor Red
    $site = new-object Microsoft.SharePoint.SPSite($SPWebURL)
    $web = $site.openweb()
    Write-Host "    >> SPWeb URL from Object:", $web.URL -foregroundcolor Green

    $SPlist = $web.Lists[$SPListName]
    Write-Host "    >> SPList to Configure:", $SPlist.Title -foregroundcolor Green
    if($SPlist.EnableVersioning -eq $true)
    {
        $SPlist.MajorVersionLimit = $MaintainMajorVersionNumber;
        if($EnableMinorVersionYesNo -eq $true)
        {
            $SPlist.EnableMinorVersions = $EnableMinorVersionYesNo
            $SPlist.MajorWithMinorVersionsLimit = $MaintainMinorVersionNumber;
        }
    }
    else
    {
        $SPlist.EnableVersioning = $True
        $SPlist.MajorVersionLimit = $MaintainMajorVersionNumber;
        if($EnableMinorVersionYesNo -eq $true)
        {
            $SPlist.EnableMinorVersions = $EnableMinorVersionYesNo
            $SPlist.MajorWithMinorVersionsLimit = $MaintainMinorVersionNumber;
        }
    }
    $SPlist.Update();
    $web.Dispose();
    $site.Dispose();
}

function Force-Apply-Versionning-OnItems([string]$SPWebURL, [string]$SPListName, [int]$MaintainVersionNumber)
{
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null

    Write-Host " -------------------------------------------------------- "
    Write-Host "SPWeb URL to configure:", $SPWebURL  -foregroundcolor Red
    $site = new-object Microsoft.SharePoint.SPSite($SPWebURL)
    $web = $site.openweb()
    Write-Host "    >> SPWeb URL from Object:", $web.URL -foregroundcolor Green

    $SPlist = $web.Lists[$SPListName]
    Write-Host "    >> SPList to Configure:", $SPlist.Title -foregroundcolor Green
    Write-Host "    >> Total Item to check:", $SPlist.Items.Count -foregroundcolor Green

    foreach ($SPitem in $SPlist.Items)
    {
        Write-Host "       >>>>>> File Item:", $SPitem.Name, "- Version Total:", $SPItem.Versions.count -foregroundcolor Magenta
        $currentVersionsCount = $SPItem.Versions.count
        if($currentVersionsCount -gt $MaintainVersionNumber)
        {
            $SPitem.SystemUpdate()
        }
    }
    $SPlist.Update();
   
    $web.Dispose();
    $site.Dispose();

}

Configure-Versionning-SPList   http://MyWebApplication/sites/MySPSite/MySPWeb "My List Name" 50  $false 0
Force-Apply-Versionning-OnItems 
http://MyWebApplication/sites/MySPSite/MySPWeb "My List Name" 50 

Any choice you selected, the only objective is to reduce as much as possible the time required to migrate the content.

Romelard Fabrice [MVP]

Source:

 

French version: