6

I am trying to enable version history on a regular task list programmatically which is possible from the UI, but how can this be achieved in code?

I assumed setting EnableVersioning = true would do the trick, but it does not work.

According to msdn:

Although the EnableVersioning property is defined on the SPList class, versioning is only available for document library lists."

3 Answers 3

3

I've used code like this in a console app where I was reconfiguring some lists. This enables versioning and sets limits.

var depts = web.Lists.TryGetList("Depts");
                    if (depts != null)
                    {
                        depts.EnableModeration = true;
                        depts.EnableVersioning = true;
                        depts.MajorVersionLimit = 1;
                        depts.MajorWithMinorVersionsLimit = 1;
                        depts.DraftVersionVisibility = DraftVisibilityType.Author;
                        depts.Update();
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("Versioning enabled on the department list successfully.");
                        Console.WriteLine("");
                    }
3

You probably forgot to call SPList.Update method to apply the changes, for example:

var tasksList = web.Lists.TryGetList("Tasks");
tasksList.EnableVersioning = true;
tasksList.Update();
0

Versioning on lists can also be Enabled/Disabled using PowerShell. I have used this CSOM PowerShell function to enable versioning on a custom list. Since its a CSOM you need not run this on server, i.e. you can execute this function remotely provided you have installed Client APIs

Try{
    Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll'
    Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
    Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll'
    Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll'
}

catch {
    Throw "Unable to load SharePoint Client runtime"
}

function Enable-VersioningOnList()
{
    param(
        [Parameter(Mandatory=$true)][string]$url,
        [Parameter(Mandatory=$true)][System.Net.NetworkCredential]$credentials,
        [Parameter(Mandatory=$true)][string]$listName
    )
    begin{
        try
        {
            #get Client Object
            $context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
            $context.Credentials = $credentials
            $web = $context.Web 
            $context.Load($web) 
            $context.ExecuteQuery()

            #Retrieve List
            $List = $context.Web.Lists.GetByTitle($listName)
            $context.Load($List)
            $context.ExecuteQuery()


        }
        catch
        {
            Write-Host "Error while getting context. Error -->> "  + $_.Exception.Message -ForegroundColor Red
        }
    }
    process{
        try
        {
            # To disable use $false else if to enable use $true
            $list.EnableVersioning = $true
            $list.Update()
            $context.ExecuteQuery()
            Write-Host "Versioning enabled on " $List.Title " successfully" -ForegroundColor Green

        }
        catch
        {
            Write-Host "Error while disabling versioning. Error -->> "  + $_.Exception.Message -ForegroundColor Red
        }
    }
    end{
        $context.Dispose()
    }
}

Usage:

$credentials = Get-Credential
Enable-VersioningOnList "http://yoursite" $credentials "ListTitle"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.