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.
- ------------------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 is it, looks simple but it relieves us developers of a large burden. Enjoy. Until then, Cheers.