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

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

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

SharePoint Online:使用 PowerShell 将 XML 导入到列表


要求: 将 XML 导入到 SharePoint Online 列表。

PowerShell 将 XML 导入到 SharePoint Online 列表

使用 PowerShell 将数据从 XML 文件导入到 SharePoint Online 列表可能是从另一个系统迁移数据或更新现有列表数据的有用方法。本文介绍如何使用 PowerShell 将 XML 文件导入 SharePoint Online 列表。

以下是将 XML 导入到 SharePoint Online 列表的 PowerShell:


#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)
    $Ctx.ExecuteQuery()

    #import xml file
    [xml]$ProjectXmlFile = Get-Content $XMLFile
 
    #Iterate through each "Project" node of the XML file
    Foreach ($XMLProject in $ProjectXmlFile.projects.project)
    {
        #Add New List Item
        $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
        $NewProject = $List.AddItem($ListItemInfo)

        #Map XML fields to SharePoint Online List Fields - Internal Name
        $NewProject["ProjectID"] = $XMLProject.id
        $NewProject["Title"] = $XMLProject.ProjectName
        $NewProject["ProjectDescription"] = $XMLProject.description
        $NewProject["ProjectManager"] = $web.EnsureUser($XMLProject.manager)
        $NewProject["Domain"] = $XMLProject.domain
        $NewProject["Budget"] = $XMLProject.budget
        $NewProject["StartDate"] = $XMLProject.startdate
   
        $NewProject.Update()
 
        Write-Host "Project $($XMLProject.id) has been Imported to the Projects List!"
    }
    $Ctx.ExecuteQuery()
}
Catch {
        write-host -f Red "Error Importing XML Data into List!" $_.Exception.Message
}

我的 XML 结构:


<?xml version="1.0" encoding="UTF-8"?>
<projects>
    <project id="PMO-1250">
        <projectname>SharePoint 2016 Upgrade</projectname>
        <description>Migration Project to Move from SharePoint 2010 to SharePoint 2016</description>
        <manager>[email protected]</manager>
        <budget>$75000</budget>
        <domain>IT Applications</domain>
        <startdate>1/1/2016</startdate>
    </project>
    <project id="PMO-1121">
        <projectname>Azure Data center Migration</projectname>
        <description>Azure Data center Migration Project</description>
        <manager>[email protected]</manager>
        <budget>$120000</budget>
        <domain>IT Infrastructure</domain>
        <startdate>2/1/2017</startdate>
    </project>
</projects>

这是我在运行脚本之前创建的列表结构:

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

这是针对 SharePoint 本地部署编写的另一篇文章:PowerShell to Import XML to SharePoint List

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

取消回复欢迎 发表评论:

关灯