当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 将列表数据导出到 XML

作者:精品下载站 日期:2024-12-14 14:50:31 浏览:18 分类:玩电脑

SharePoint Online:使用 PowerShell 将列表数据导出到 XML


要求: 将 SharePoint Online 列表数据导出到 XML 文件。

PowerShell 将 SharePoint Online 列表导出为 XML

使用 PowerShell 将 SharePoint Online 列表导出到 XML 文件可能是备份列表或将列表数据传输到另一个系统的有用方法。在本文中,我们将逐步介绍使用 PowerShell 将 SharePoint Online 列表导出到 XML 文件的步骤。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/"
$ListName="Projects"
$XMLFile ="C:\Temp\ProjectData.xml"

Try{
    #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
    $Ctx.Load($Ctx.Web)
    $List = $Web.Lists.GetByTitle($ListName)
    $ListFields =$List.Fields
    $Ctx.Load($ListFields)
    $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

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

    #Add XML Declaration
    $Declaration = $XMLDocument.CreateXmlDeclaration("1.0","UTF-8",$null)
    $XMLDocument.AppendChild($Declaration)
    
    #Create Root Node
    [System.XML.XMLElement]$XMLRoot=$XMLDocument.CreateElement($ListName)
 
    #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)
        $XMLRoot.AppendChild($ItemElement)

        #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)
            $FieldElement.Set_InnerText($Item[$Field.InternalName])
            #Append to Root node
            $ItemElement.AppendChild($FieldElement)
        }
    }
    # Append Root Node to XML
    $XMLDocument.AppendChild($XMLRoot)

    #Save XML File
    $XMLDocument.Save($XMLFile)

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

通过PowerShell脚本生成的XML文件:

[玩转系统] SharePoint Online:使用 PowerShell 将列表数据导出到 XML

此 PowerShell 脚本从所有列中提取数据并将其导出为 XML。您可能必须单独处理人员选择器、查找等列。


#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
    }

    $FieldElement.Set_InnerText($FieldValue)
    #Append to Root node
    $ItemElement.AppendChild($FieldElement)
} 

请参阅:如何使用 PowerShell 将 SharePoint Online 列表项导出到 CSV?

最后但并非最不重要的一点:
您可以使用 RPC 方法或 REST 调用将 SharePoint Online 列表导出为 XML

  • https://YourDomain.sharepoint.com/_vti_bin/owssvr.dll?Cmd=Display&List={GUID}&XMLDATA=TRUE ,例如https://Crescent.sharepoint.com/_vti_bin/owssvr.dll?Cmd=Display&List={0d4ff78d-8759-46b4-94bd-d5ee5f7564a8}&XMLDATA=TRUE。要获取完整的列表结构,请使用:https://Crescent。 sharepoint.com/_vti_bin/owssvr.dll?Cmd=ExportList&List={0d4ff78d-8759-46b4-94bd-d5ee5f7564a8}
  • 使用 REST API 调用将 SharePoint 列表数据获取为 XML 格式:https://Your-Domain.sharepoint.com/_api/lists/getbytitle(‘List-Name’)/items? , 例如https://Crescent.sharepoint.com/_api/lists/getbytitle(‘项目’)/items?

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯