SharePoint Online: Import XML to List using PowerShell

Requirement:  Import XML to SharePoint Online List

PowerShell to Import XML to SharePoint Online List
Here is the PowerShell to import XML in to SharePoint Online list
#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
$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
}

My XML Structure:
<?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>Salaudeen@Crescent.com</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>Peter@Crescent.com</manager>
  <budget>$120000</budget>
  <domain>IT Infrastructure</domain>
  <startdate>2/1/2017</startdate>
 </project>
</projects>

Here is my List structure I've created before running the script:
sharepoint online import xml to list using powershell

Here is my another post written for SharePoint On-premises: PowerShell to Import XML to SharePoint List
SharePoint Online: Import XML to List using PowerShell SharePoint Online: Import XML to List using PowerShell Reviewed by Salaudeen Rajack on January 19, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.