Versioned items in SharePoint allow us to revert back to a previous version when desired. In this recipe, we will revert the publishing page we created in the Creating a publishing web part page recipe.
Follow these steps to revert a publishing page:
When a previous version of an item is restored, it copies that version and makes it the newest version. The version prior to the current one will be saved as an old version.
SharePoint list and library items may also be reverted with PowerShell or code using the server-side object model.
Follow these steps to revert a publishing page using PowerShell:
Get-SPWeb
Cmdlet as follows:$web = Get-SPWeb "http://sharepoint/publishing"
$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$camlQuery = "<Where><Eq><FieldRef Name='Title'></FieldRef><Value Type='Text'>PowerShell Page</Value></Eq></Where>" $page = $pubWeb.GetPublishingPages($camlQuery)
$page.ListItem.File.CheckOut()
$page.ListItem.Versions.RestoreByLabel("1.0")
$page.ListItem.File.CheckIn("Reverted to 1.0")
Dispose
method to discard the SPWeb
object as follows:$web.Dispose()
Follow these steps to revert a publishing page with code using the server-side object model:
using
statement as follows:using (var site = new SPSite("http://sharepoint/publishing"))
using
statement.using (var web = site.OpenWeb())
var pubWeb = PublishingWeb.GetPublishingWeb(web);
var camlQuery = "<Where><Eq><FieldRef Name='Title'></FieldRef><Value Type='Text'>Code Page</Value></Eq></Where>"; var page = pubWeb.GetPublishingPages(camlQuery).First();
page.ListItem.File.CheckOut();
page.ListItem.Versions.RestoreByLabel("1.0");
page.ListItem.File.CheckIn("Reverted to 1.0");
3.135.204.0