Automate Microsoft 365 Migrations with Cloudiway Rest Apis
Tags: teams, migration, sharepoint, microsoft365
Published: 2024-03-19 14:48:59

Cloudiway is a Cloud migration platform  that allows to [migrate your data between tenants](https://cloudiway.com/solutions/) (Mailboxes, Drives, Teams, sites ).

This article explains how to use the Cloudiway Rest Apis to automate the migration.

**Prerequisites**

- You need to create an account in the [Cloudiway platform ](Https://portal.cloudiway.com).
- Authentication is done using a PAT (Personal Access Token). You need to create a PAT

**Procedure to create a PAT:**
In the upper right, click on ACCOUNT then API

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8eto3nsho1br96naxg0c.png)
Click on Create a new Token and fill the information:

- Give a name to your PAT
- Define an expiration date
- Define the CRUD operations that are authorized


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2zieofkl1921ymj7w6e.png)

Click on Create and save your PAT for the next steps.

**Create your first application:**
The Swagger definition of the Cloudiway APIs can be found here: [Swagger definitions](https://api-production.cloudiway.com/index.html)


Set the project that you wish to access.
You need to define your project ID. 
In the swagger URL, login ( Click on Authorize and paste your PAT).
Then call the /projects endpoint and note the projectID of your choice.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qahpb837iashv5w549cl.png)

You are now ready to programmatically manipulate the Cloudiway platform:
You global settings will look like this:
$global:HOSTURL = 'https://api-production.cloudiway.com'
$userId = 'Name@cloudiway.com' #Your Login
$userPass = 'YourPassword' #Your Password
# Login
$global:TOKEN = 'Your PAT'

Identify the project you want to work on and insert the projectId here
$global:PROJID = 1234

## Create a user programmatically

```
  $FirstName = "Test"
  $lastName = "User"
  $SourcePoolId = 1
  $TargetPoolId = 2
  $SourceEmail = "user@source.com"
  $TargetEmail = "user@target.com"
  $TargetRecipientType = 1 #1 = onedrive, 2= sharepoint
  $TargetChannelName = "" #not used
  $TargetFolder= "" #not used
  $TargetLibrary= "" #not used
  $this.TargetSiteName= "" #not used
  
  function GetHeader(){
  $Header = @{
      "accept" = "*/*"
      "Authorization" = "Bearer $global:BEARERTOKEN" 
      "projectId" = $global:PROJID
     }
     return $Header
  } 
  function CreateFileUser(){    
        $BodyJson = @{
            firstName = $this.FirstName
            lastName = $this.LastName
            sourcePoolId = $this.SourcePoolId
            targetPoolId = $this.TargetPoolId
            sourceEmail = $this.SourceEmail
            targetEmail = $this.TargetEmail
            targetRecipientType = $this.TargetRecipientType
            targetChannelName = $this.TargetChannelName
            targetFolder =$this.TargetFolder
            targetLibrary = $this.TargetLibrary
            targetSiteName=$this.TargetSiteName         
        }    
        $Parameters = @{
            Method = "Post"
            Uri = "$global:HOSTURL/ap1/File"
            Headers = GetHeader
            ContentType = "application/json"
            Body = ($BodyJson | ConvertTo-Json)
        }
        
        $this.ResponseObject = Invoke-FrontAPI($Parameters) | ConvertFrom-Json
        $this.ResponseJson = $this.ResponseObject | ConvertTo-Json -depth 100   
    }
```
## Start the migration programmatically
Let's define a job class:

```
class Jobs
{
    $ResponseObject
    [String]$ResponseJson
    [Int]$ProjectId
    [Int]$BatchId
    [Int]$JobType
    [Int]$ProductType
    [string]$StartJobsList
    [string]$StopJobsList


    StartJobs(){
        
        $PType=$this.ProductType
        $JType=$this.JobType
        

        $Parameters = @{
            Method = "Post"
            Uri = "$global:HOSTURL/ap1/Jobs/StartJobs/$PType/$JType"
            Headers = GetHeader
            ContentType = "application/json"
            Body = $this.StartJobsList
        }
        
        $this.ResponseObject = Invoke-FrontAPI($Parameters) | ConvertFrom-Json
        $this.ResponseJson = $this.ResponseObject | ConvertTo-Json -depth 100   
    }
}
#Now we just have to call it:
function StartFileJobs{
  param ([int]$JobType, [string]$ListIDs)
  Write-Host "Start Jobs: " -ForegroundColor Yellow
  $Jobs = [Jobs]::new()
  $Jobs.StartJobsList= $ListIDs
  $Jobs.ProductType = 6 #6 is for File, do not modify
  $Jobs.JobType = $JobType 
  $Jobs.StartJobs()
  Write-Host $Jobs.ResponseJson
}
```
## Further reading
[Full rest api documentation is available here](https://cloudiway.com/documentation/restapis-documentation/).