SharePoint Online: Export List Data to XML using PowerShell

Requirement: Export SharePoint Online List data to XML file.

PowerShell to Export SharePoint Online List to XML
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Set parameter values
$XMLFile ="C:\Temp\ProjectData.xml"

    #Get Credentials to connect
    $Cred= Get-Credential
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    #Get the web & List objects
    $Web = $Ctx.Web
    $List = $Web.Lists.GetByTitle($ListName)
    $ListFields =$List.Fields
    $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 

    #Create new XML File
    [System.XML.XMLDocument]$XMLDocument=New-Object System.XML.XMLDocument

    #Add XML Declaration
    $Declaration = $XMLDocument.CreateXmlDeclaration("1.0","UTF-8",$null)
    #Create Root Node
    #Iterate through each List Item in the List
    Foreach ($Item in $ListItems)
        #Add child node "Item"
        $ItemElement = $XMLDocument.CreateElement("Item")
        $ItemElement.SetAttribute("ID", $Item.ID)

        #Loop through each column of the List Item
        ForEach($Field in $ListFields | Where {$_.Hidden -eq $false -and $_.ReadOnlyField -eq $false -and $_.Group -ne "_Hidden"})
            $FieldElement = $XMLDocument.CreateElement($Field.InternalName)
            #Append to Root node
    # Append Root Node to XML

    #Save XML File

    Write-Host -f Green "List Items Exported to XML!"
Catch {
        write-host -f Red "Error Exporting List Data to XML!" $_.Exception.Message

XML file generated through PowerShell script:
sharepoint online export list to xml
This PowerShell script extracts data from all columns and exports to XML. You may have to handle columns like People Picker, Lookup, etc separately.
#Loop through each columns of the List Item
ForEach($Field in $ListFields | Where {$_.Hidden -eq $false -and $_.ReadOnlyField -eq $false -and $_.Group -ne "_Hidden"})
    $FieldElement = $XMLDocument.CreateElement($Field.InternalName)
    $FieldValue = $Item[$Field.InternalName]

    #Check for User Field
    If($Field.GetType().Name -eq "FieldUser")
        #Get the People picker Column value of the list item
        $UserFieldValue = [Microsoft.SharePoint.Client.FieldUserValue]$Item[$Field.InternalName] 
        #Get the Email of the User 
        $FieldValue = $UserFieldValue.Email #$FieldValue.LookupValue

    #Append to Root node

Last but not least:
You can export SharePoint Online list to XML using RPC method or REST Calls
  •{GUID}&XMLDATA=TRUE ,E.g.{0d4ff78d-8759-46b4-94bd-d5ee5f7564a8}&XMLDATA=TRUE .To get the Complete List Structure, use:{0d4ff78d-8759-46b4-94bd-d5ee5f7564a8}
  • Use REST API Call to Get SharePoint List Data into XML format:'List-Name')/items? , E.g.'Projects')/items?


  1. If I use RPC method to export SharePoint Online list to XML, will i be able to save it in a location on my local computer using a powershell script?

    1. Yes! Exactly that's what this script does. Set $XMLFile ="C:\Temp\ProjectData.xml" according to your requirement.


Please Login and comment to get your questions answered!

Powered by Blogger.