SharePoint Online: Files operation with PnP Powershell

First, you will have to connect to your SharePoint Online Site

Example: Connect to SPO

$siteurl = "https://yourspsite.sharepoint.com"  
Connect-SPOnline -Url $siteurl  
$ctx = Get-SPOContext  

Example: How to retrieve/find files
*Site wide, not recommended

Find-SPOFile -Match *

From a list

Find-SPOFile -Match * -List "Documents"

From a site

$web = Get-SPOWeb -Identity "Subsite"
Find-SPOFile -Match * -Web $web

From a list in a site

$web = Get-SPOWeb -Identity "Subsite"
Find-SPOFile -Match * -Web $web -List "Pages"

Access file properties

$web = Get-SPOWeb -Identity "Subsite"
$files = Find-SPOFile -Match * -Web $web -List "Pages"
foreach($file in $files) {
     #get file properties
     $file.Title
     $file.Name
     $file.ServerRelativeUrl
     #etc...
}

Example: How to download file

Get-SPOFile -ServerRelativeUrl "Relative path to your file, e.g: /documents/testing.docx" `
            -Path "Local folder path, e.g: C:\temp" `
            -FileName "Download file name, e.g: test.docx"

Example: To check out the file

Set-SPOFileCheckedOut -Url "Relative path to your file, e.g: /documents/testing.docx"  

Example: To check-in the file

Set-SPOFileCheckedIn -Url "Relative path to your file, e.g: /documents/testing.docx" `
                     -CheckinType MajorCheckIn -Comment "Check"

Example: To add a single file

Add-SPOFile -Path "Path to source file" `
            -Folder "Path to destination folder" -Checkout

You can publish the file by adding -Publish, and publishing comments by adding -PublishingComment

Add-SPOFile -Path "Path to source file" `
            -Folder "Path to destination folder" `
            -Publish -PublishComment "Published by PnP script"

Example: To add a folder

Upload folder function

Function UploadFolder {
	Param(
	  [Parameter(Mandatory=$True)]
	  [String]$targetWeb,

	  [Parameter(Mandatory=$True)]
	  [String]$targetDir,

	  [Parameter(Mandatory=$True)]
	  [String]$path 

	)
	## get the full path to the source folder
	$fullPath = (Get-Item $path).FullName
	## recursively get all files in the directory
	$files = Get-ChildItem -Path $path -Recurse -File
	## upload each file to the directory specified before
	foreach($currentFile in $files)
	{
		## get relative path of documents
		$relativeDirectory = $currentFile.DirectoryName.Replace($fullPath, "").Replace("\","/")
		$uploadDirectory = $targetDir + $relativeDirectory
		Write-Host "Uploading" $currentFile.Name "to" $uploadDirectory "..." -ForegroundColor Yellow
		## Upload the document
		Add-SPOFile -Path $currentFile.FullName -Folder $uploadDirectory -Checkout
		Write-Host "Uploaded" $currentFile.Name "to" $uploadDirectory -ForegroundColor Green
	}
}

Example: How to use the function

UploadFolder -targetWeb "https://yourspsite.sharepoint.com/sites/test/" -targetDir "Style Library/folder" `
	     -path ("path to source folder in local drive")

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s