From 852785618f6c0a39902cf7954e3ecaf0770425b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20Hartvig=20Gr=C3=B8nbech?= Date: Thu, 10 Oct 2024 11:34:51 +0200 Subject: [PATCH 1/2] add support --- .../MSFTTenantMediaSetAPI.Page.al | 90 +++++++++++++++++++ .../MSFTTenantMediaSetIDsAPI.Page.al | 28 ++++++ .../MoveTenantMedia.ps1 | 69 +++++++++++++- 3 files changed, 184 insertions(+), 3 deletions(-) create mode 100644 samples/CloudMigration/MoveTenantMediaBetweenTenantsWithAPI/MSFTTenantMediaSetAPI.Page.al create mode 100644 samples/CloudMigration/MoveTenantMediaBetweenTenantsWithAPI/MSFTTenantMediaSetIDsAPI.Page.al diff --git a/samples/CloudMigration/MoveTenantMediaBetweenTenantsWithAPI/MSFTTenantMediaSetAPI.Page.al b/samples/CloudMigration/MoveTenantMediaBetweenTenantsWithAPI/MSFTTenantMediaSetAPI.Page.al new file mode 100644 index 00000000..2b60ccd6 --- /dev/null +++ b/samples/CloudMigration/MoveTenantMediaBetweenTenantsWithAPI/MSFTTenantMediaSetAPI.Page.al @@ -0,0 +1,90 @@ +page 58213 MSFTTenantMediaSetAPI +{ + PageType = API; + ApplicationArea = All; + UsageCategory = Administration; + SourceTable = "Tenant Media Set"; + EntityName = 'tenantMediaSet'; + EntitySetName = 'tenantMediaSet'; + APIPublisher = 'MSFT'; + APIGroup = 'moveData'; + APIVersion = 'v1.0'; + DelayedInsert = true; + Permissions = tabledata "Tenant Media Set" = rmid; + + layout + { + area(Content) + { + repeater(TenantMedia) + { + field(id; Rec.ID) + { + Caption = 'id'; + } + field(mediaID; Rec."Media Index") + { + Caption = 'Media Index'; + } + + field(base64ContentTxt; Base64ContentTxt) + { + Caption = 'Base64ContentTxt'; + } + + field(companyName; Rec."Company Name") + { + Caption = 'Company Name'; + TableRelation = Company.Name; + } + + } + } + } + + trigger OnInsertRecord(BelowxRec: Boolean): Boolean + begin + SetBase64Text(); + Rec.Insert(); + exit(false); + end; + + trigger OnAfterGetRecord() + begin + GetBase64Text(); + end; + + local procedure GetBase64Text() + var + Base64Convert: Codeunit "Base64 Convert"; + ContentInStream: InStream; + ContentOutStream: OutStream; + TempBlob: Codeunit "Temp Blob"; + begin + Clear(Base64ContentTxt); + Rec.CalcFields("Media ID"); + if not Rec."Media ID".HasValue() then + exit; + + TempBlob.CreateOutStream(ContentOutStream); + Rec."Media ID".ExportStream(ContentOutStream); + TempBlob.CreateInStream(ContentInStream); + Base64ContentTxt := Base64Convert.ToBase64(ContentInStream); + end; + + local procedure SetBase64Text() + var + Base64Convert: Codeunit "Base64 Convert"; + ContentInStream: InStream; + ContentOutStream: OutStream; + TempBlob: Codeunit "Temp Blob"; + begin + TempBlob.CreateOutStream(ContentOutStream); + Base64Convert.FromBase64(Base64ContentTxt, ContentOutStream); + TempBlob.CreateInStream(ContentInStream); + Rec."Media ID".ImportStream(ContentInStream, ''); + end; + + var + Base64ContentTxt: text; +} \ No newline at end of file diff --git a/samples/CloudMigration/MoveTenantMediaBetweenTenantsWithAPI/MSFTTenantMediaSetIDsAPI.Page.al b/samples/CloudMigration/MoveTenantMediaBetweenTenantsWithAPI/MSFTTenantMediaSetIDsAPI.Page.al new file mode 100644 index 00000000..8731a2be --- /dev/null +++ b/samples/CloudMigration/MoveTenantMediaBetweenTenantsWithAPI/MSFTTenantMediaSetIDsAPI.Page.al @@ -0,0 +1,28 @@ +page 58214 MSFTTenantMediaSetIDsAPI +{ + PageType = API; + ApplicationArea = All; + UsageCategory = Administration; + SourceTable = "Tenant Media Set"; + EntityName = 'tenantMediaSetIds'; + EntitySetName = 'tenantMediaSetIds'; + APIPublisher = 'MSFT'; + APIGroup = 'moveData'; + APIVersion = 'v1.0'; + DelayedInsert = true; + Permissions = tabledata "Tenant Media Set" = rmid; + + layout + { + area(Content) + { + repeater(TenantMedia) + { + field(id; Rec.ID) + { + Caption = 'id'; + } + } + } + } +} \ No newline at end of file diff --git a/samples/CloudMigration/MoveTenantMediaBetweenTenantsWithAPI/MoveTenantMedia.ps1 b/samples/CloudMigration/MoveTenantMediaBetweenTenantsWithAPI/MoveTenantMedia.ps1 index 2291e707..a199900e 100644 --- a/samples/CloudMigration/MoveTenantMediaBetweenTenantsWithAPI/MoveTenantMedia.ps1 +++ b/samples/CloudMigration/MoveTenantMediaBetweenTenantsWithAPI/MoveTenantMedia.ps1 @@ -20,8 +20,9 @@ $script:TokenExpirationTime = (Get-Date) function Copy-TenantMedia ( - [int] $startIndex = 0, # Use this parameters if you want to run the script in parallel. Start the first scripts by specifying e.g. $maxCount 10.000 and second script with $startIndex 10.000 and $maxCount 10.000 - [int] $maxCount = 0 + [int] $startIndex = 0, # Use this parameters if you want to run the script in parallel. Start the first scripts by specifying e.g. $maxCount 10.000 and second script with $startIndex 10.000 and $maxCount 10.000 + [int] $maxCount = 0 + [bool] $transferMediaSets = $false ) { # Setup URLs @@ -29,11 +30,15 @@ function Copy-TenantMedia $sourceCompanyId = Get-SourceCompanyURL -EnvironmentUlr $script:SourceEnvironmentUlr $sourceMediaAPIUrl = $script:SourceEnvironmentUlr + '(' + $sourceCompanyId + ')/tenantMedia' $sourceMediaIDsAPIUrl = $script:SourceEnvironmentUlr + '(' + $sourceCompanyId + ')/tenantMediaIds' + $sourceMediaSetAPIUrl = $script:SourceEnvironmentUlr + '(' + $sourceCompanyId + ')/tenantMediaSet' + $sourceMediaSetIDsAPIUrl = $script:SourceEnvironmentUlr + '(' + $sourceCompanyId + ')/tenantMediaSetIds' Write-Host "Using company with ID $sourceCompanyId as source" $targetCompanyId = Get-SourceCompanyURL -EnvironmentUlr $script:TargetEnvironmentUlr $targetMediaAPIUrl = $script:TargetEnvironmentUlr + '(' + $targetCompanyId + ')/tenantMedia' - $targetMediaIDsAPIUrl = $script:TargetEnvironmentUlr + '(' + $targetCompanyId + ')/tenantMediaIds' + $targetMediaIDsAPIUrl = $script:TargetEnvironmentUlr + '(' + $targetCompanyId + ')/tenantMediaIds' + $targetMediaSetAPIUrl = $script:TargetEnvironmentUlr + '(' + $targetCompanyId + ')/tenantMediaSet' + $targetMediaSetIDsAPIUrl = $script:TargetEnvironmentUlr + '(' + $targetCompanyId + ')/tenantMediaSetIds' Write-Host "Using company with ID $targetCompanyId as target" # Get Tenant Media Records to copy @@ -41,6 +46,13 @@ function Copy-TenantMedia $sourceTenantMediaIDs = Get-TenantMediaIDs -MediaIDsAPIUrl $sourceMediaIDsAPIUrl [System.Collections.ArrayList] $itemsToMove = $sourceTenantMediaIDs | Select-Object -Property id | Sort-Object -Property id + $MediaSetitemsToMove = [System.Collections.ArrayList]@() + if ($transferMediaSets) + { + $sourceTenantMediaSetIDs = Get-TenantMediaIDs -MediaIDsAPIUrl $sourceMediaSetIDsAPIUrl + $MediaSetitemsToMove = $sourceTenantMediaSetIDs | Select-Object -Property id | Sort-Object -Property id + } + $targetTenantMediaIDs = Get-TenantMediaIDs -MediaIDsAPIUrl $targetMediaIDsAPIUrl $ExistingTenantMedia = New-Object System.Collections.Specialized.OrderedDictionary @@ -57,6 +69,24 @@ function Copy-TenantMedia } } + $ExistingTenantMediaSets = New-Object System.Collections.Specialized.OrderedDictionary + if ($transferMediaSets) + { + $targetTenantMediaSetIDs = Get-TenantMediaIDs -MediaIDsAPIUrl $targetMediaSetIDsAPIUrl + if($targetTenantMediaSetIDs) + { + if($targetTenantMediaSetIDs.value.Count -gt 0) + { + [System.Collections.ArrayList] $targetTenantMediaSetIDs = $targetTenantMediaSetIDs | Select-Object -Property id | Sort-Object -Property id + + for ($i = 0; $i -lt $targetTenantMediaSetIDs.Count; $i++) + { + $ExistingTenantMediaSets.Add($targetTenantMediaSetIDs[$i].id,'') + } + } + } + } + $endIndexOfItemsToMove = $itemsToMove.Count if($maxCount) @@ -93,6 +123,39 @@ function Copy-TenantMedia $response = Invoke-PostMethod -Uri $targetMediaAPIUrl -Token $Token -Body $tenantMediaBody } } + + if ($transferMediaSets) + { + + $endIndexOfItemSetssToMove = $MediaSetitemsToMove.Count + + if($maxCount) + { + $endIndexOfItemSetssToMove = $startIndex + $maxCount + } + + # Move selected records + for($i = $startIndex; $i -le $endIndexOfItemSetssToMove; $i++) + { + $mediaIDToMove = $MediaSetitemsToMove[$i].id + if (-not $ExistingTenantMediaSets.Contains($MediaSetitemsToMove[$i].id)) + { + $sourceURl = $sourceMediaSetAPIUrl + '(' + $mediaIDToMove + ')' + $Token = Get-AADToken + $sourceResponse = Invoke-GetMethod -Uri $sourceURl -Token $Token + + $tenantMediaBody = @{ + id="$($sourceResponse.id)"; + companyName=$($sourceResponse.companyName); + mediaID=$($sourceResponse.mediaID); + base64ContentTxt=$($sourceResponse.base64ContentTxt); + } + + Write-Host "Moving Media with ID " $tenantMediaBody.id " File Name " $tenantMediaBody.fileName + $response = Invoke-PostMethod -Uri $targetMediaSetAPIUrl -Token $Token -Body $tenantMediaBody + } + } + } } function Get-TenantMediaIDs From 0ff2fffeac08440e6e029a5310716bc592dd3ceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20Hartvig=20Gr=C3=B8nbech?= Date: Fri, 18 Oct 2024 11:56:58 +0200 Subject: [PATCH 2/2] fix --- .../MoveTenantMediaBetweenTenantsWithAPI/MoveTenantMedia.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/CloudMigration/MoveTenantMediaBetweenTenantsWithAPI/MoveTenantMedia.ps1 b/samples/CloudMigration/MoveTenantMediaBetweenTenantsWithAPI/MoveTenantMedia.ps1 index a199900e..34482490 100644 --- a/samples/CloudMigration/MoveTenantMediaBetweenTenantsWithAPI/MoveTenantMedia.ps1 +++ b/samples/CloudMigration/MoveTenantMediaBetweenTenantsWithAPI/MoveTenantMedia.ps1 @@ -21,7 +21,7 @@ $script:TokenExpirationTime = (Get-Date) function Copy-TenantMedia ( [int] $startIndex = 0, # Use this parameters if you want to run the script in parallel. Start the first scripts by specifying e.g. $maxCount 10.000 and second script with $startIndex 10.000 and $maxCount 10.000 - [int] $maxCount = 0 + [int] $maxCount = 0, [bool] $transferMediaSets = $false ) {