Restoring SharePoint Data from Backup using PnP PowerShell

Loading

Restoring SharePoint list data from a backup is essential for data recovery, business continuity, and maintaining data integrity. This guide will walk you through step-by-step procedures for restoring SharePoint list data from CSV files, re-importing list attachments, and restoring list schemas using PnP PowerShell.

1. Introduction

When restoring a SharePoint list from a backup, there are three key components to consider:

  • List Schema: The structure of the list (columns, data types, settings).
  • List Data: The actual data stored in the list.
  • Attachments: Files attached to list items.

PnP PowerShell simplifies the process by allowing you to restore these components efficiently.


2. Connecting to SharePoint Online

Before restoring data, connect to your SharePoint Online site.

Step 1: Connect to SharePoint

Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive
  • Replace yourtenant with your actual SharePoint tenant name.
  • Replace yoursite with the target SharePoint site.
  • The -Interactive flag prompts for login credentials.

3. Restoring SharePoint List Structure (Schema)

If the list was deleted or is missing, restore its structure first using an XML template backup.

Step 1: Define Schema Backup Path

$SchemaPath = "C:\Backup\ListSchema.xml"

Step 2: Restore the List Schema

Invoke-PnPProvisioningTemplate -Path $SchemaPath

This command recreates the list with the original structure, including columns and settings.


4. Importing List Data from CSV Backup

Once the list structure is in place, restore the list items from the CSV backup.

Step 1: Load CSV Data

$ImportPath = "C:\Backup\SharePointListBackup.csv"
$Data = Import-Csv -Path $ImportPath

Step 2: Restore Data to SharePoint List

foreach ($Item in $Data) {
Add-PnPListItem -List "YourListName" -Values @{
"Title" = $Item.Title
"Column1" = $Item.Column1
"Column2" = $Item.Column2
}
}
  • Replace "YourListName" with the actual SharePoint list name.
  • Modify "Column1", "Column2", etc., to match your list columns.

5. Restoring List Attachments

If the backup includes attachments, restore them to their respective list items.

Step 1: Define Attachment Backup Path

$AttachmentBackupPath = "C:\Backup\Attachments"

Step 2: Restore Attachments

$ListName = "YourListName"

$Data | ForEach-Object {
$ItemId = $_.ID
$AttachmentFiles = Get-ChildItem -Path "$AttachmentBackupPath" -Filter "$ItemId-*"

foreach ($File in $AttachmentFiles) {
Add-PnPListItemAttachment -List $ListName -Identity $ItemId -Path $File.FullName
}
}
  • This script loops through each list item and re-attaches the corresponding files.
  • Ensure that the file naming convention (ItemID-Filename) matches the backup process.

6. Verifying Restored Data

After restoration, verify that the list data and attachments were imported successfully.

Step 1: Retrieve and Check Restored Items

Get-PnPListItem -List "YourListName" | Format-Table Id, Title

This command lists the restored items, confirming that the data was successfully imported.

Step 2: Verify Attachments

$ItemId = 1  # Replace with an actual Item ID
Get-PnPListItemAttachment -List "YourListName" -Identity $ItemId

This command checks if the attachments for a specific list item were restored correctly.


7. Automating the Restoration Process

To automate the restoration process, save the script as a PowerShell file (Restore-SharePoint.ps1) and schedule it to run when needed.

Step 1: Save the Script

Save the complete restoration script as a .ps1 file.

Step 2: Run Automatically via Windows Task Scheduler

  1. Open Task Scheduler.
  2. Click Create Basic Task.
  3. Set Trigger (Run on demand or schedule it).
  4. Set Action to Start a Program.
  5. Choose powershell.exe and provide the script path as an argument.
  6. Save and enable the task.

Leave a Reply

Your email address will not be published. Required fields are marked *