Migrate Files and Folder to SP Online using Microsoft 365 CLI

Introduction

Are you comfortable with mac or Linux operating system ?
Do you want to upload your local files or folders to SharePoint document library? 
This article incorporate how do upload files and folders to SharePoint Online using Microsoft 365 CLI.

Why Microsoft 365 CLI?

The CLI for Microsoft 365 is a command-line interface. A PC program that cycles orders as lines of text. CLI programs are regularly utilized in the Unix world however are ordinarily utilized in cross stage situations/scenarios too.
The CLI for M365 is a cross-platform arrangement  that permits users on any stage to oversee different design settings of M365 tenant.
While building solutions for Microsoft 365 grows beyond the windows operating system, managing many of the platform settings is conceivable only through PowerShell script on Windows.
As an ever increasing number of users work on non-windows machines, It is inconvenient for them to need to utilized a windows virtual machine to design their M365 tenants.
The CLI for Microsoft 365 permits them to configure their tenants regardless of which working framework they use.

Getting Started

The CLI for Microsoft 365 is circulated as a NPM bundle. Ensure your current enviornment is set up by installing the LTS version of Node.

Download LTS version of Node
To utilize the CLI, install it globally using

npm i -g @pnp/cli-microsoft365 

Once installed you can login to the tenant you want to manage using m365 login.
To get an outline of all commands you can utilized below command

m365 help  

Here you can see there are many options available to manage the Microsoft 365 artifacts.
Login To Connect

C:\Users\Dips3>m365 login  
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code DJTCS7PCG to authenticate.  

Once login successfully done then check status using below command

C:\Users\Dips3>m365 status  
connectedAs: dips1802@dev1802.onmicrosoft.com  

Let’s get started to upload documents
M365 CLI include 2 commands.

  1. To create Folder
  2. To create File

Let’s check command to create folder

m365 spo folder add [options]  
//How to execute command with options
m365 spo folder add --webUrl httpss://dev1802.sharepoint.com/sites/M365dev --parentFolderUrl '/Shared Documents' --name 'Sample'

Output as below
PS C:\Users\Dips3> m365 spo folder add --webUrl https://dev1802.sharepoint.com/sites/CodeChallenge --parentFolderUrl '/Shared Documents' --name 'Folder1'
Exists : true
IsWOPIEnabled : false
ItemCount : 0
Name : Folder1
ProgID : null
ServerRelativeUrl: /sites/CodeChallenge/Shared Documents/Folder1
TimeCreated : 2021-06-03T20:19:56Z
TimeLastModified : 2021-06-03T20:19:56Z
UniqueId : 5e299d79-178e-4a9c-8553-ebfbdb4c306c
WelcomePage :

Options as below:

–webUrl : The Url of Site where folder will be created
–parentFolderUrl : Site relative url of the parent folder
–name: Name of the new folder to be created

Let’s check command to create file 

m365 spo file add [options]  
// How to execute command with options  
m365 spo file add --webUrl https://dev1802.sharepoint.com/sites/M365dev --folder 'Shared Documents' --path 'C:\sample.pdf'  
// How to add file to sub folder in SharePoint online  
m365 spo file add --webUrl https://dev1802.sharepoint.com/sites/M365dev --folder 'Shared Documents/folder1' --path 'C:\sample.pdf'  

–webUrl : The URL of Site where file will be created
–folder: Site relative URL of the parent folder
–path: Local path to the file upload
Let’s get started to create script to upload folder and file to SharePoint Online

Output will be as below.
I am using windows 10. So,  I write this script in Windows PowerShell.
You can write up script on any editor or framework.
You need to call command to create folder and file with required parameter.


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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.