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.
Clear-Host
Write-Host "Enter SharPoint Online User name : Example [email protected]"
$UserName = "[email protected]"
$Password = "****"

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

$listTemplateId=100

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

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) 
 $ClientContext.Load($List)
 $ClientContext.ExecuteQuery()
 #Adds an item to the list
 $InvFile="TeamsCSV.csv" 
 # Get Data from Inventory CSV File 
 $FileExists = (Test-Path $InvFile -PathType Leaf) 
 if ($FileExists) 
 { 
 "Loading $InvFile for processing…" 
 $CSVData = import-csv $InvFile 
 } 
 else 
 { 
 "$InvFile not found – stopping import!" 
 exit 
 } 
 # 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
 $item.Update()
 $ClientContext.ExecuteQuery()
 }
}

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}
 if($list)
 {
 Write-Warning "List already exists!!. Deleting list "$info.Title
 $list.DeleteObject()
 $Context.ExecuteQuery()
 }
 Write-Host "Creating list.."$info.Title
 $list = $Context.Web.Lists.Add($info)
 $Context.ExecuteQuery()
 Write-Host "Success!!"
}
Function Export-SchemaFiles()
{
 $context = Get-SPOContext -Url $Url -UserName $UserName -Password $Password
 $Context.Load($Context.Web.Lists)
 $Context.Load($Context.Web.ListTemplates)
 $Context.ExecuteQuery()
 $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 
 } 
 $context.Dispose() 
}
Write-Host "Starting Export"

Export-SchemaFiles
#Export-TeamInfo

Write-Host "Export Completed successfully !!"

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.