Office 365 SharePoint Easy Migration Part 1

Hi Everyone,
Today I will be sharing scripts which will be useful for migration of data to SharePoint online (Download PDF for FREE). 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.

So lets scripting to automate solution.

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

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

We can have multiple <UploadFile> tag to define multiple SharePoint file location 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

Read some SharePoint online tutorials:


Hope this will help. I will be sharing code to import data in CSV format to SharePoint List.

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