Skip to content

Instantly share code, notes, and snippets.

@pkskelly
Last active October 12, 2015 15:07
Show Gist options
  • Save pkskelly/097bb3e6aa3cafe4cb08 to your computer and use it in GitHub Desktop.
Save pkskelly/097bb3e6aa3cafe4cb08 to your computer and use it in GitHub Desktop.
Add list items to SharePoint Online - Office 365 List using CSOM from CSV file.
# =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
# Script: Add-SPListItemsFromCSV.ps1
#
# Author: Pete Skelly
# Twitter: ThreeWillLabs
# http://www.threewill.com
#
# Description: Add list items to SharePoint Online - Office 365 List using CSOM from CSV file.
#
# WARNING: Script provided AS IS with no warranty. Your mileage will vary. Use
# this script on a production list AT YOUR OWN RISK.
#
# =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
param(
[string]
$siteUrl,
[string]
$userId,
[string]
$listName,
[string]
$csvName
)
# ---------------------------------------------------------
# Load SharePoint 2013 CSOM libraries.
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
$password = Read-Host -Prompt "Enter password" -AsSecureString
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $password)
$ctx.Credentials = $credentials
$list = $ctx.get_web().get_lists().getByTitle($listName);
## Step 1. collect column names from the header row of the file
$csvFilePath = Join-Path -Path $scriptPath -ChildPath $csvName
if (Test-Path "$csvFilePath")
{
# Step 2. collect the rows for the list
Write-Host "Importing CSV data from $csvFilePath "
$listItems = import-csv -Path "$csvFilePath"
$recordCount = @($listItems).count;
Write-Host -ForegroundColor Yellow "There are $recordCount list items to process"
Write-Host "Please wait..."
# Step 3. Add the list items
for($rowCounter = 1; $rowCounter -le $recordCount - 1; $rowCounter++)
{
$curItem = @($listItems)[$rowCounter];
Write-Progress -id 1 -activity "Adding List Item" -status "Inserting item $rowCounter of $recordCount list items." -percentComplete ($rowCounter*(100/$recordCount));
# # Create list item.
$itemCreateInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$newItem = $list.addItem($itemCreateInfo);
$newItem.set_item('Title', $curItem.Summary);
$newItem.set_item('FieldName1', $curItem.ColumnName1);
$newItem.set_item('FieldName2', $curItem.ColumnName2);
# More fields / columns as needed... may improve if I revisit...
#simple hack for Lookup lists...
#$newItem.set_item('LookupFieldName', $curItem.LookupValues); #Id1;#Value1;#Id2;#Value2;#Id3;#Value3... may improve if I revisit...
$newItem.update();
$ctx.Load($newItem)
$ctx.ExecuteQuery()
}
}
else
{
Write-Host "Could not load file path $csvFilePath "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment