Reverting publishing content to a previous version

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.

How to do it...

Follow these steps to revert a publishing page:

  1. Navigate to the SharePoint list or library that contains the item to be approved in your preferred web browser.
  2. Select the item by clicking on the checkmark on the item.
  3. Select Check Out from the FILES tab on the ribbon.
  4. Select the item by clicking on the checkmark of the item.
  5. Select Version History from the FILES tab on the ribbon.
    How to do it...
  6. Select the drop-down menu from the Modified Date option to select the version you want to revert.
  7. Select Restore as shown in the following screenshot:
    How to do it...
  8. Click on OK.
  9. Check in the item to complete the process using the Check In option.

How it works...

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.

There's more...

SharePoint list and library items may also be reverted with PowerShell or code using the server-side object model.

Reverting publishing content using PowerShell

Follow these steps to revert a publishing page using PowerShell:

  1. Get the site using the Get-SPWeb Cmdlet as follows:
    $web = Get-SPWeb "http://sharepoint/publishing"
    
  2. Get the publishing site from the SharePoint site.
    $pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
    
  3. Get the publishing page using the following CAML query:
    $camlQuery = "<Where><Eq><FieldRef Name='Title'></FieldRef><Value Type='Text'>PowerShell Page</Value></Eq></Where>"
    
    $page = $pubWeb.GetPublishingPages($camlQuery)
    
  4. Check out the publishing page as follows:
    $page.ListItem.File.CheckOut()
    
  5. Restore the item to the specified version.
    $page.ListItem.Versions.RestoreByLabel("1.0")
    
  6. Check in the publishing page as follows:
    $page.ListItem.File.CheckIn("Reverted to 1.0")
    
  7. Use the Dispose method to discard the SPWeb object as follows:
    $web.Dispose()
    

Reverting publishing content with code using the server-side object model

Follow these steps to revert a publishing page with code using the server-side object model:

  1. Open the site collection containing the site in a using statement as follows:
    using (var site = new SPSite("http://sharepoint/publishing"))
  2. Open the site in a using statement.
    using (var web = site.OpenWeb())
  3. Get the publishing site from the SharePoint site.
    var pubWeb = PublishingWeb.GetPublishingWeb(web);
  4. Get the publishing page with the following CAML query:
    var camlQuery = "<Where><Eq><FieldRef Name='Title'></FieldRef><Value Type='Text'>Code Page</Value></Eq></Where>";
    
    var page = pubWeb.GetPublishingPages(camlQuery).First();
  5. Check out the publishing page using the following line of code:
    page.ListItem.File.CheckOut();
  6. Restore the item to the specified version.
    page.ListItem.Versions.RestoreByLabel("1.0");
  7. Check in the publishing page using the following line of code:
    page.ListItem.File.CheckIn("Reverted to 1.0");
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.204.0