Export SharePoint List Items to XML using PowerShell

Sometime back, I wrote a PowerShell script to import from a XML file to SharePoint list, Import XML File Data into SharePoint List using PowerShell. Now the requirement is in reverse: Export SharePoint List data to XML file!

PowerShell script to Export SharePoint List Items to XML:

How to export SharePoint list to XML? Here in the PowerShell script!

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set these three variables accordingly
$WebURL  = "https://projects.crescent.com/"
$ListName = "External Projects"
$XMLFilePath = "E:\data\ExternalProjects.xml"

#Get the Web
$web = Get-SPWeb $WebURL
#Get the List
$ProjectList = $web.Lists[$ListName]

#Create a XML File
$XMLFile = New-Object System.Xml.XmlDocument
#Add XML Declaration
[System.Xml.XmlDeclaration] $xmlDeclaration = $XMLFile.CreateXmlDeclaration("1.0", "UTF-16", $null);
$XMLFile.AppendChild($xmlDeclaration) | Out-Null
   
 #Create Root Elemment "Projects"
$ProjectsElement = $XMLFile.CreateElement("Projects")
 
 #Iterate through each list item and send Rows to XML file
foreach ($Item in $ProjectList.Items)
{
  #Add "Project" node under "Projects" Root node
  $ProjectElement = $XMLFile.CreateElement("Project")
  #Add "ID" attribute to "Project" element
  $ProjectElement.SetAttribute("id", $Item["ID"])
  $ProjectsElement.AppendChild($ProjectElement)  | Out-Null
  
  #Populate Each Columns
  #Add "Description" node under "Project" node
  $DescriptionElement = $XMLFile.CreateElement("description"); 
  $DescriptionElement.InnerText = $Item["Description"]
  #Append it to "Project" node
  $ProjectElement.AppendChild($DescriptionElement) | Out-Null
  
  #Add "Project Manager" element under "Project" node
  $managerElement = $XMLFile.CreateElement("manager"); 
  $managerElement.InnerText = $Item["Project Manager"]
  #Append it to "Project" node
  $ProjectElement.AppendChild($managerElement) | Out-Null
  
  #Add "Cost" element under "Project" node
  $CostElement = $XMLFile.CreateElement("cost"); 
  $CostElement.InnerText = $Item["Cost"]
  #Append it to "Project" node
  $ProjectElement.AppendChild($CostElement) | Out-Null
}
#Close the Root Element
$XMLFile.AppendChild($ProjectsElement) | Out-Null
#Save all changes
$XMLFile.Save($XMLFilePath) 

XML file generated:

export sharepoint list to xml powershell

Another nifty trick would be: Export SharePoint list to Ms-Access, and then from there, export tables to XML!
sharepoint export list item to xml

You can also export/view SharePoint List items to XML format using RPC call, refer: Retrieve SharePoint List data in XML format

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

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