Click here to Skip to main content
15,881,561 members
Articles / Programming Languages / PowerShell
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
11 Jul 2014CPOL 15.3K   1   2
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)


Written By
Argentina Argentina
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Lydia Gabriella16-Jul-14 10:43
Lydia Gabriella16-Jul-14 10:43 
GeneralRe: My vote of 5 Pin
MANPREET SINGH8-Aug-14 6:59
MANPREET SINGH8-Aug-14 6:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.