Thursday, 13 December 2018

SharePoint CSOM to add data from CSV

PowerShell and Excel

PowerShell is very simple and powerful. Microsoft Office products like Word, Excel, PowerPoint has changed the world and become a part of every household.Advanced COM programming is possible, which I use for advanced operations on Word. Lets see how PowerShell is helpful with  single line code with Excel.

Command Available
Export-CSV
$data| Export-csv -Path $csvPath -Append

Import-CSV
$newFruits=Import-Csv -Path $csvPath

Let is say you have a Fruits data along with color information, we will display the fruits information in Excel as shown below:

$yourFolder="C:\demo\thangu"
$csvPath=$yourFolder+"\fruitsCsv.csv"
#New powerShell Table
$hashContent = [PSCustomObject]@{Name="Apple";Color="Red"}
$hashContent | Export-csv -Path $csvPath -Force
$hashContent = [PSCustomObject]@{Name="Mango";Color="Yellow"}
$hashContent | Export-csv -Path $csvPath -Append
$newFruits=Import-Csv -Path $csvPath



Sample Application to move data to external system(SharePoint)


SharePoint List Addition
Lets try to add some items from CSV to SharePoint List having fruits data.I used CSOM to read the data from CSV file created above and used the following CSOM code to move the data from CSV to Fruits list.

foreach($fruit in $newFruits){
$newItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$newItem=$psList.AddItem($newItemInfo)

$newItem["Title"] = $fruit.Name
$newItem["Color"] = $fruit.Color
$newItem.Update()
$Context.ExecuteQuery()
}


No comments:

Post a Comment