Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 11 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

MANPREET SINGH

Argentina Argentina
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberLydia Gabriella16-Jul-14 10:43 
GeneralRe: My vote of 5 PinmemberMANPREET SINGH8-Aug-14 6:59 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 11 Jul 2014
Article Copyright 2014 by MANPREET SINGH
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid