Office 365 SharePoint Easy Migration Part 2

Hi Everyone,
Sometimes it happens the data will be available in excel sheet or any format and we need to add those details to SharePoint List. I will be sharing code to add items to list through the script. You can also check Part-1 of this article series.

We have to create CSV files and provide the list field name as a header in CSV file and place the data to be added to the SharePoint List.

Please find the attached CSV files for details.

Find the below XML and Script to make the work done.

In XML file i am providing list field name.



<?xml version=”1.0″ encoding=”utf-8″?>
<WebSite Url=”https://microsft.sharepoint.com/sites/test/” >
<ImportData>
<ListData Path=”ListData\TestItems.csv” List=”TestList1″>
<Field Name=”Title”/>
<Field Name=”Link”/>
<Field Name=”Icon”/>
<Field Name=”Position”/>
<Field Name=”Order”/>
</ListData>
</ImportData>
</WebSite>




Script -:
cls

$0 = $MyInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$xmlFilePath = $(“$dp0\Inputs.xml”)
$xmldata = [xml](Get-Content($xmlFilePath));

$username = “samtest@microsoft.com”
$password = Read-Host -Prompt “Please enter your password” -AsSecureString
$url = $xmldata.WebSite.Url

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials

Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Publishing.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Taxonomy.dll”

if (!$clientContext.ServerObjectIsNull.Value)
{
Write-Host “Connected to SharePoint Online site: ‘$Url'” -ForegroundColor Green

$web = $clientContext.Web
$clientContext.Load($web)
$clientContext.ExecuteQuery()

}

function ImportData([Microsoft.SharePoint.Client.Web] $web)
{
foreach($data in $xmldata.WebSite.ImportData.ListData)
{
$path=$data.Path
$csvPath = $(“$dp0\$path”)
$csv = Import-CSV $csvPath

$list = $web.Lists.GetByTitle($data.List)
$clientContext.Load($list)
$clientContext.ExecuteQuery()

foreach ($row in $csv) {

$itemCreateInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation;
$item = $list.AddItem($itemCreateInfo);
foreach($field in $data.Field){

$name=$field.Name
$item[$name] = $row.$name;

}
$item.Update();
}
$clientContext.ExecuteQuery()
}
}
ImportData $web

Read some SharePoint online tutorials:


Hope this will be useful.

Thanks,
Sambita


You May Also like the Following SharePoint Online Tutorials:

About Sambita Rath

I have 9 years of exprience into SharePoint implementation, architecture, Administrator, development and Training.Designing Information Architecture in SharePoint 2007, 2010,2013 and Office 365.

View all posts by Sambita Rath →

Leave a Reply