Read XML Files and Add Them To SharePoint 2010 Using PowerShell

I know it has been a long time but what to say, no comments. You know what the professional life is like but I am back with a bang.

Let's start up again. Today I'll be showing you a PowerShell script that picks up the data from XML files and put it into a SharePoint list.

This is an important PowerShell that you will see since it will be used to migrate .Net applications to SharePoint. Most .Net applications use XML files to store data. So here in this PowerShell I will be iterating through all the XML files of a complete application and put them into SharePoint.

Seems easy, right? But it was tough for me.

So the title of this article is Read XML Files and Add Them To SharePoint 2010 Using 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.

  1. ------------------Save the below in a notepad , save it as .ps1 file and run it------------  
  3. Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null  
  4. [System.Xml.XmlDocument] $XmlDoc = new-object System.Xml.XmlDocument  
  5. Get-ChildItem “Put the path of all xml files here” *.xml -recurse |  
  6. % {  
  7.     $XmlDoc = [xml](Get-Content $_.fullname)  
  9.     $path = $XmlDoc.ExportedContentItem.path  
  10.     $name = $  
  11.     $GUID = $XmlDoc.ExportedContentItem.ID  
  13.     $out = $path + "/" + $name + "`t" + $GUID  
  14.     Write-Host -ForegroundColor Green $_.fullname " is reading to the list item"  
  16.     $url = "Put the site Url u want to add into"  
  17.     $listTitle = "List name"  
  18.     $web = Get-SPWeb $url  
  19.     $list = $web.Lists.TryGetList($listTitle)  
  20.     if ( $list -ne $null)  
  21.     {  
  22.         #get data from the xml tags and read them by assigning them  
  23.         $EmpIdadding = $XmlDoc.GetElementsByTagName("ID")  
  24.         $Nameadding = $XmlDoc.GetElementsByTagName("Name")  
  25.         Foreach ($empid in $EmpIdadding){  
  26.         # Get InnerXML value of ID node and assign it to string  
  27.         $strempid=$empid.InnerXML.ToString()  
  28.     }  
  29.     Foreach ($name in $Nameadding){  
  30.         # Get InnerXML value of name node and assign it to string  
  31.         $strname=$name.InnerXML.ToString()  
  32.     }  
  33.     #Assign the string values to the list item  
  34.     $item =$list.AddItem();  
  35.     $item["name"] = $strname  
  36.     $item["empid"] = $strempid  
  37.     $item.Update()  
  38.     Write-Host -ForegroundColor Green $_.fullname " is added successfully to the list item"  
  39.   }  
  40. }  
  41. $Web.Update()  
  42. $Web.AllowUnsafeUpdates = $false  
  43. $Web.Dispose() 

That is it, looks simple but it relieves us developers of a large burden. Enjoy. Until then, Cheers.

Up Next
    Ebook Download
    View all
    View all