使用 PowerShell 将 SharePoint 列表项导出到 XML
不久前,我编写了一个 PowerShell 脚本来从 XML 文件导入到 SharePoint 列表,使用 PowerShell 将 XML 文件数据导入到 SharePoint 列表。现在的要求是相反的:将 SharePoint 列表数据导出到 XML 文件!
将 SharePoint 列表项导出到 XML 的 PowerShell 脚本:
如何将 SharePoint 列表导出为 XML?在 PowerShell 脚本中!
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 文件:
另一个巧妙的技巧是:将 SharePoint 列表导出到 Ms-Access,然后从那里将表导出到 XML!
您还可以使用 RPC 调用将 SharePoint 列表项导出/查看 XML 格式,请参阅:检索 XML 格式的 SharePoint 列表数据

