Office 365 SharePoint Easy Migration

Hi Everyone, Today I will be sharing scripts that will be useful for migration of data to SharePoint Online. Sometimes we have the documents, files from any other application which we need to move to SharePoint.It’s quite a time taking process if we have to upload the files manually to SharePoint Document Libraries.

I had a requirement where the client provided huge documents from a legacy application and we had to move all the files to SharePoint document libraries.

Move Files to SharePoint document libraries

So lets scripting to automate the solution.

Please place the input values below XML. Create a Local folder named “Documents” where you have kept the .ps1 file. You can provide any name and accordingly update the LocalFolderPath value in the XML input file.

Provide a SharePoint library or folder inside the library in SPfolderPath XML property.

We can have multiple <UploadFile> tags to define multiple SharePoint file locations and respective document folder location.

<?xml version="1.0" encoding="utf-8"?>
<WebSite Url="https://microsoft.sharepoint.com/sites/test" >
<UploadFiles>
<UploadFile LocalFolderPath="Documents" SPfolderPath="SampleDoc"/>
</UploadFiles>
</WebSite>

Script Code-

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 UploadFiles([Microsoft.SharePoint.Client.Web] $web)
{
foreach($uploadFile in $xmldata.WebSite.UploadFiles.UploadFile)
{
$folder=$web.GetFolderByServerRelativeUrl($uploadFile.SPfolderPath)
$clientContext.Load($folder);
$clientContext.ExecuteQuery();
$location=$uploadFile.LocalFolderPath
$FolderLocation = $(“$dp0\$location")
foreach ($File in (dir $FolderLocation))
{
$FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $File
$Upload=$folder.Files.Add($FileCreationInfo)
$clientContext.Load($Upload)
$clientContext.ExecuteQuery()
}
}
}

UploadFiles $web

Add items to list through the script in SharePoint

Sometimes it happens the data will be available in an excel sheet or any format and we need to add those details to SharePoint List. I will be sharing code to add items to the list through the script.

We have to create CSV files and provide the list field name as a header in the 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 the XML file I am providing a 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

You  may like following SharePoint migration tutorials:

This SharePoint tutorial, we learned how to move files from SharePoint document libraries and how to add items to list through the script in SharePoint Online Office 365.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

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.

>