Sunday, February 23, 2014

Import XML File Data into SharePoint List using PowerShell

Requirement: We've an utility from third-party system that generates project data in XML format. We had to import those data to a list in the PMO site. In other words: We've to import from XML file to SharePoint list.

Here is a sample XML file, generated by the tool:

<?xml version="1.0"?>
<projects>
 <project id="PMO.1120">
  <description>GIS upgrade 2013 </description>
  <manager>global\E372440</manager>
  <cost>$35000</cost>
  <startdate>1/1/2014</startdate>
 </project>
 <project id="PMO.1121">
  <description>HRIT Asset Life Cycle Automation</description>
  <manager>AMER\E132321</manager>
  <cost>$63000</cost>
  <startdate>1/1/2014</startdate>
 </project>
</projects>  

Solution: Using PowerShell, lets import XML data into SharePoint list.

PowerShell script to read from XML and import to SharePoint list:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set these two variables accordingly
$WebURL  = "http://project.crescent.com/"
$ListName = "External Projects"
$XMLFilePath = "D:\data\ExternalProjects.xml"

#Get the Web
$web = Get-SPWeb $WebURL
#Get the List
$ProjectList = $web.Lists[$ListName]

#import xml file
[xml]$ProjectXmlFile = Get-Content $XMLFilePath

foreach ($XMLProject in $ProjectXmlFile.projects.project)
 {
        $NewProject = $ProjectList.Items.Add()
  
        $NewProject["Project ID"] = $XMLProject.id
        $NewProject["Description"] = $XMLProject.description
        #Set the People Picker Field
        $NewProject["Project Manager"] = $web.EnsureUser($XMLProject.manager)
        $NewProject["Cost"] = $XMLProject.cost
        $NewProject["Start Date"] = $XMLProject.startdate
  
        $NewProject.Update()

        Write-Host "Project $($XMLProject.id) has been Added to External Projects list!"
 }
That's all! we are done importing XML data to sharepoint list with PowerShell!!

If you are looking for a way to Export SharePoint list items to XML, Refer: http://www.sharepointdiary.com/2014/03/export-list-items-to-xml-using-powershell.html



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:

2 comments :

  1. Hi
    Is it possible for SharePoint Online? Let me know your comments on importing data from XML to SharePoint Online.
    Thanks

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...