Sunday, March 23, 2014

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:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set these three variables accordingly
$WebURL  = "http://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!
export sharepoint 2010 list to xml file
You can also export/view SharePoint List items to XML format using RPC call, refer: Retrieve SharePoint List data in XML format



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...