SharePoint Online : Exporting Lists using Remote PowerShell

Exporting list schema’s in to SharePoint across the environments is always tricky. With the deprecation of Sandbox solutions and no go for farm solutions, provisioning lists through PowerShell will always be a savior.

You can get the List Schema from Visual Studio by creating a list instance and copying the schema.xml
Even though the below approach through Script supports SharePoint Online, with few modifications it can serve On Prem as well.
The provisioning of lists through Power Shell script is explained in detail at the below.
Write-Host "Enter SharPoint Online User name : Example [email protected]"
$UserName = "[email protected]"
$Password = "****"

$Url = "https://{siteurl}/sites/ridhvi/"



Function Get-SPOContext([string]$Url,[string]$UserName,[string]$Password)
 Write-Host "Loading Context.."
 $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
 $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
 $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
 return $context

Function Export-TeamInfo()
 $ClientContext = Get-SPOContext -Url $Url -UserName $UserName -Password $Password 
 # Get List
 $ListName = "WGSTeams"
 $List = $ClientContext.Web.Lists.GetByTitle($ListName) 
 #Adds an item to the list
 # Get Data from Inventory CSV File 
 $FileExists = (Test-Path $InvFile -PathType Leaf) 
 if ($FileExists) 
 "Loading $InvFile for processing…" 
 $CSVData = import-csv $InvFile 
 "$InvFile not found – stopping import!" 
 # Loop Create List Item
 #Adds an item to the list
 $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation 
 foreach ($row in $CSVData)
 $item = $List.AddItem($ListItemInfo) 
 $item["Title"] = $row.Title
 $item["CommentCount"] = $row.CommentCount
 $item["LikeCount"] = $row.LikeCount
 $item["Logo"] = $row.Logo
 $item["DownloadLogo"] = $row.DownloadLogo
 $item["TeamLogo"] = $row.TeamLogo
 $item["DocumentDisplayLogo"] = $row.DocumentDisplayLogo
 $item["ShortDescription"] = $row.ShortDescription
 $item["Description"] = $row.Description

Function Create-ListSchema([Microsoft.SharePoint.Client.ClientContext]$Context, [int]$listTemplateId,[string] $schemaXmlFilePath)
 $schemaXml = [xml] (Get-Content $schemaXmlFilePath)
 $template = $Context.Web.ListTemplates | WHERE { $_.ListTemplateTypeKind -eq $listTemplateId }
 $info = New-Object Microsoft.SharePoint.Client.ListCreationInformation
 $info.Title = $schemaXml.List.Title
 $info.Url = $schemaXml.List.Url
 $info.CustomSchemaXml = $schemaXml.OuterXml
 $info.TemplateType = $listTemplateId
 $info.TemplateFeatureId = $template.FeatureId
 $list = $Context.Web.Lists | where{$_.Title -eq $info.Title}
 Write-Warning "List already exists!!. Deleting list "$info.Title
 Write-Host "Creating list.."$info.Title
 $list = $Context.Web.Lists.Add($info)
 Write-Host "Success!!"
Function Export-SchemaFiles()
 $context = Get-SPOContext -Url $Url -UserName $UserName -Password $Password
 $ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path 
 $SchemaDir = $ScriptDir+"\Schemas\"
 #Write-Host "Current script directory is $SchemaDir"
 $files = Get-ChildItem $SchemaDir
 for ($i=0; $i -lt $files.Count; $i++)
 $schemaXmlFilePath = $files[$i].FullName 
 Create-ListSchema -Context $context -listTemplateId 100 -schemaXmlFilePath $schemaXmlFilePath 
Write-Host "Starting Export"


Write-Host "Export Completed successfully !!"
© 2023 | WordPress Theme: Annina Free by CrestaProject.