Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / PowerShell

Read .xml Files and Add them into SharePoint 2010 through PowerShell

5.00/5 (1 vote)
11 Jul 2014CPOL 15.8K  
How to read XML files and add them into SharePoint 2010 through Powershell

Introduction

Today, I’ll be showing you a PowerShell script which picks up the data from XML files and puts it into SharePoint list.

This is an important PowerShell which you will see as it will be used to migrate .NET applications to SharePoint. Most of the .NET applications use XML files to store data. So here in this PowerShell, I will be looping all the XML files of a complete application and put them into SharePoint.

Seems easy, right? But was tough for me.

So the title of this article will be:

Read .xml Files and add them into SharePoint 2010 through PowerShell

Open SharePoint 2010 Management Shell by going to Start >> All Programs >>SharePoint >>Microsoft SharePoint 2010 Products >> SharePoint 2010 Management Shell (Run as Administrator).

Run the following script.

------------------Save the below in a Notepad , save it as .ps1 file and run it------------

Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null 
[System.Xml.XmlDocument] $XmlDoc = new-object System.Xml.XmlDocument

Get-ChildItem "Put the path of all xml files here" *.xml -recurse | 
% { 
    $XmlDoc = [xml](Get-Content $_.fullname)

    $path =  $XmlDoc.ExportedContentItem.path
    $name =  $XmlDoc.ExportedContentItem.name
    $GUID =  $XmlDoc.ExportedContentItem.ID

    $out = $path + "/" + $name + "`t" + $GUID
Write-Host -ForegroundColor Green $_.fullname " is  reading to the list item"     

$url = "Put the site Url u want to add into"
$listTitle = "List name" 
$web = Get-SPWeb $url
$list = $web.Lists.TryGetList($listTitle)
if ( $list -ne $null)
    {
#get data from the xml tags and read them by assigning them
                                         $EmpIdadding = $XmlDoc.GetElementsByTagName("ID")
			$Nameadding = $XmlDoc.GetElementsByTagName("Name")
			
Foreach ($empid in $EmpIdadding){
 # Get InnerXML value of ID  node and assign it to string
 $strempid=$empid.InnerXML.ToString()
}
Foreach ($name in $Nameadding){
 # Get InnerXML value of name node and assign it to string
 $strname=$name.InnerXML.ToString()
}

#Assign the string values to the list item
            $item  =$list.AddItem();
                                           $item["name"] = $strname
			$item["empid"] = $strempid
                                           $item.Update()			
	Write-Host -ForegroundColor Green $_.fullname " is added successfully to the list item"  	  
	}
        }
$Web.Update() 
$Web.AllowUnsafeUpdates = $false
$Web.Dispose()

That's it, looks simple, but eases us developers of a lot of burden. Enjoy! Cheers!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)