-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCDSImport.psm1
744 lines (643 loc) · 22.2 KB
/
CDSImport.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# Copyright © Microsoft Corporation. All Rights Reserved.
# This code released under the terms of the
# Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
# Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment.
# THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
# We grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and distribute the object code form of the Sample Code, provided that.
# You agree:
# (i) to not use Our name, logo, or trademarks to market Your software product in which the Sample Code is embedded;
# (ii) to include a valid copyright notice on Your software product in which the Sample Code is embedded;
# and (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or lawsuits, including attorneys’ fees, that arise or result from the use or distribution of the Sample Code
Set-StrictMode -Version 4.0
Import-Module -Name SqlServer
Import-Module -Name Microsoft.XRM.Data.PowerShell
function Import-CDSAccessWebApp
{
<#
.SYNOPSIS
Imports Access Web App schema and data into a Common Data Service database.
.DESCRIPTION
Import-CDSAccessWebApp reads the schema of an Access Web App (AWA) from SQL Azure,
creates the same schema in the Common Data Service (CDS), and then imports the data.
.PARAMETER XRM
If you have an existing connection to an XRM/CDS instance by calling Connect-CrmOnlineDiscovery or Connect-CrmOnline, you can pass it in here. If not, you will be prompted to login and create a connection.
.PARAMETER SQLServer
Name of the SQL Server hosting the AWA. You can obtain this information from the File menu, Connections, in Access when connected to the AWA.
.PARAMETER SQLDatabase
Name of teh SQL Database for the AWA.
.PARAMETER SQLUserName
Name of the SQL User with at least read permissions to the AWA.
.PARAMETER SQLPassword
The password for the SQLUserName.
.PARAMETER Publisher
The publisher string to use when creating entities and fields. By default, the string "AWA" is used.
.PARAMETER DisplaNamePrefix
A prefix to include at the beginning of the entity display names. Paired with the Publisher, use this to test the script and import into a new set of entities within the same databsae.
.PARAMETER OneEntity
Used for testing purposes, name of a single entity to load. No relationships are created.
.PARAMETER UsePresets
Read and write configuration settings to a local file. Use for testing and to import to multiple databases without prompts.
.PARAMETER NoCreateEntities
Do not create entities, only load data.
.PARAMETER NoLoadData
Do not load data, only create entities.
.PARAMETER OrganizationOwned
Use this switch to have the organization own the created entities. By default, entities are owned by the user who created them.
.PARAMETER MaxMemoLength
Maximum size for memo fields. Default is 100,000.
.PARAMETER MaxImageSize
Maximum size of an image to import. Default is 12,000,000.
.PARAMETER Culture
Default is "en-us".
.PARAMETER LCID
Default is 1033.
.EXAMPLE
Import-CDSAccessWebApp -SQLServer awa.database.windows.net -SQLDatabase db_xxxx -SQLUserName db_xxxx_ExternalReader -SQLPassword xxxx
#>
[CmdletBinding()]
param
(
[system.object] $XRM = $null,
[parameter(Mandatory=$true)] [string] $SQLServer,
[parameter(Mandatory=$true)] [string] $SQLDatabase,
[parameter(Mandatory=$true)] [string] $SQLUsername,
[parameter(Mandatory=$true)] [string] $SQLPassword,
[string]$Publisher = "AWA",
[string]$DisplayNamePrefix = "",
[string]$OneEntity = "",
[switch]$UsePresets,
[switch]$NoCreateEntities,
[switch]$NoLoadData,
[switch]$OrganizationOwned,
[int]$MaxMemoLength = 100000,
[int]$MaxImageSize = 12000000,
[string]$Culture = "en-us",
[int]$LCID = 1033
)
function XRMExecute
{
Param( $cer )
if( -not $NoCreateEntities )
{
for( $retry = 0; $retry -lt 5; $retry++ )
{
try
{
return( $XRM.Execute( $cer ) )
}
catch
{
Write-Host -foregroundcolor Red $_.Exception.Message
Write-Host -foregroundcolor Red "Retry in 10 seconds..."
}
Start-Sleep -s 10
}
Write-Host -foregroundcolor Red "Giving up, exiting..."
break
}
}
function XRMCreateNewRecord
{
Param( $ln, $cnr )
if( -not $NoLoadData )
{
for( $retry = 0; $retry -lt 5; $retry++ )
{
try
{
$x = $XRM.CreateNewRecord( $ln, $cnr )
if( $x -ne "00000000-0000-0000-0000-000000000000" )
{
return( $x );
}
else
{
Write-Host -foregroundcolor Red ("CreateNewRecord failed: "+$xrm.lastcrmerror)
}
}
catch
{
Write-Host -foregroundcolor Red $_.Exception.Message
}
Write-Host -foregroundcolor Red "Retry in 10 seconds..."
Start-Sleep -s 10
}
Write-Host -foregroundcolor Red "Giving up, exiting..."
break
}
else
{
return( "1234" )
}
}
function ReadSQLImage
{
Param( [string] $table, [string] $id )
$query = "select datalength([Image]) as binlen from "+$table+" where id="+$id
$binlen = ReadSQL -query $query
$len = $binlen["binlen"]
if( $len -gt $MaxImageSize )
{
write-host -foregroundcolor Red ("Image for id="+$id+" with a size of "+$len+" is larger than maximum allowed size of "+$MaxImageSize+" bytes). Use -MAxImageSize argument to change the maximum.")
return $null
}
else
{
$query = "select [Image] from "+$table+" where id="+$id
$image = ReadSQL -query $query -maxbinlen $len
return $image["Image"]
}
}
function ReadSQL
{
Param( [string] $query, [int] $maxbinlen=1024 )
$r = Invoke-Sqlcmd -Query $query -ServerInstance $SQLServer -Database $SQLDatabase -Username $SQLUsername -Password $SQLPassword -MaxBinaryLength $maxbinlen
return $r
}
function LogicalName
{
Param( [string] $name )
$noprefix = $false;
if( $name -imatch "EntityImage" )
{
$noprefix = $true
}
return $PluralService.Singularize( @(if( $noprefix ) { "" } else { $Publisher }) + $name ).ToLower() -replace '[\s\-]',''
}
function SchemaName
{
Param( [string] $name )
$noprefix = $false;
if( $name -imatch "EntityImage" )
{
$noprefix = $true
}
return $PluralService.Singularize( @(if( $noprefix ) { "" } else { $Publisher }) + (Get-Culture).TextInfo.ToTitleCase( $name ) ) -replace '[\s\-]',''
}
function SingularDisplayName
{
Param( [string] $name )
$s = $DisplayNamePrefix + $PluralService.Singularize( $name )
$r = new-object Microsoft.Xrm.Sdk.Label( $s, $lcid )
return $r
}
function PluralDisplayName
{
Param( [string] $name )
$s = $DisplayNamePrefix + $PluralService.Pluralize( $name )
$r = new-object Microsoft.Xrm.Sdk.Label( $s, $lcid )
return $r
}
function DisplayName
{
Param( [string] $name )
$s = $DisplayNamePrefix + $name
$r = new-object Microsoft.Xrm.Sdk.Label( $s, $lcid )
return $r
}
Add-Type -assembly System.Data.Entity.Design
$PluralService = [System.Data.Entity.Design.PluralizationServices.PluralizationService]::CreateService( $Culture )
if( $XRM -eq $null -and -not $NoCreateEntities -and -not $NoLoadData )
{
$XRM = Connect-CrmOnlineDiscovery -InteractiveMode
if( $XRM -eq $null )
{
write-host -foregroundcolor Red "No connection to XRM, exiting..."
break
}
}
if( -not ($Publisher -match "_$") )
{
$Publisher = $Publisher+"_"
}
if( $OrganizationOwned )
{
$Ownership = [microsoft.xrm.sdk.metadata.OwnershipTypes]::OrganizationOwned
}
else
{
$Ownership = [microsoft.xrm.sdk.metadata.OwnershipTypes]::UserOwned
}
#========================================================================================================================
Write-Host "Reading SQL Schema..."
[array]$tables = ReadSQL -query "
select t.name, t.object_id
from sys.tables t join sys.schemas s on t.schema_id = s.schema_id
where t.type_desc = 'USER_TABLE' and s.name = 'Access'
and t.name <> 'Trace' and t.name <> 'ActionEvents?' and t.name <> 'ActionEventArguments?'
and right( t.name, 7 ) <> '?Images'
"
[array]$columns = ReadSQL -query "
select c.object_id, c.column_id, c.name, c.max_length, c.precision, c.scale,
t.name as 'type', c.is_nullable, c.is_identity,
(select u.definition from sys.computed_columns u
where u.column_id = c.column_id and u.object_id = c.object_id) as 'computed_expr',
(select kc.referenced_object_id from sys.foreign_keys k join sys.foreign_key_columns kc
on k.object_id = kc.constraint_object_id and kc.parent_column_id = c.column_id
where k.parent_object_id = c.object_id) as 'lookup_object_id',
cp.Properties, cp.DefaultValue, cp.ComputedValue
from sys.columns c
join sys.types t on c.user_type_id = t.user_type_id
left join AccessSystem.ColumnProperties cp on
c.name COLLATE DATABASE_DEFAULT = cp.ColumnName COLLATE DATABASE_DEFAULT and cp.ObjectId =
(select aso.ID from AccessSystem.Objects aso join Sys.Tables st on
aso.ObjectName COLLATE DATABASE_DEFAULT = st.name COLLATE DATABASE_DEFAULT
where st.object_id = c.object_id and aso.ObjectTypeNumber = 100)
order by c.object_id, c.column_id
"
$TableColumns = @{}
$TableIndex = @{}
$identity = @{}
$TableColumnNames = New-Object System.Collections.Generic.List[System.Object]
For( $t = 0; $t -lt $tables.length; $t++ )
{
$table = $tables[$t]
$TableColumns[$table["object_id"]] = New-Object System.Collections.Generic.List[System.Object]
$TableIndex[$table["object_id"]] = $t
}
For( $c = 0; $c -lt $columns.length; $c++ )
{
$column = $columns[$c]
if( $TableIndex[$column["object_id"]] -ne $null )
{
$TableColumns[$column["object_id"]].Add( $c )
$a = "{0}:{1}:{2}:{3}" -f $tables[$TableIndex[$column["object_id"]]]["name"], $column["name"], $column["type"], $column["object_id"]
$TableColumnNames.Add( $a )
if( $column["is_identity"] )
{
$identity[$column["object_id"]] = $column["name"]
}
}
}
$LoadOrder = New-Object System.Collections.Generic.List[System.Object]
Do
{
$m = 0
ForEach( $table in $tables )
{
if( ! $LoadOrder.Contains($table["object_id"]) )
{
$l = 1
ForEach( $ci in $TableColumns[$table["object_id"]] )
{
$column = $columns[$ci]
if( -not [String]::IsNullOrEmpty($column["lookup_object_id"].ToString()) )
{
if( ! $LoadOrder.Contains($column["lookup_object_id"]) )
{
$l = 0
}
}
}
if( $l )
{
$LoadOrder.Add( $table["object_id"] )
$m = 1
}
}
}
}
while( $m )
$PrimaryColumn = @{}
$SQLPresets = $SQLDatabase + ".xml"
if( (Test-Path $SQLPresets) -and $UsePresets )
{
$p = Import-CliXML $SQLPresets
$match = 1
if( $p.TableColumnNames.Count -eq $TableColumnNames.Count )
{
for( $t = 0; $t -lt $TableColumnNames.Count; $t++ )
{
if( $p.TableColumnNames[$t] -ne $TableColumnNames[$t] )
{
$match = 0;
}
}
}
if( $match )
{
Write-Host " Configuration presets read from $SQLPresets..."
$PrimaryColumn = $p.PrimaryColumn
}
}
if( $PrimaryColumn.count -eq 0 )
{
Write-Host "Select Primary Name fields..."
For( $t = 0; $t -lt $tables.length; $t++ )
{
$table = $tables[$t]
$tc = $TableColumns[$table["object_id"]]
" {0}" -f $table["name"]
$nums = @()
For( $c = 0; $c -lt $tc.count; $c++ )
{
$col = $columns[$tc[$c]]
if( ( [String]::IsNullOrEmpty($col["lookup_object_id"].ToString()) -and $col["type"] -eq "nvarchar" -and $col["max_length"] -le 440 -and $col["max_length"] -ge 0 ) -or $col["is_identity"] )
{
$nums += $c
" [{0}] {1} {2}" -f ([string]$nums.Count).PadLeft(2), $columns[$tc[$c]]["name"], $(if($col["is_identity"]) { "(autonumber)" } else {""})
}
}
$nums += -1
" [{0}] {1}" -f ([string]$nums.Count).PadLeft(2), "New <Primary Name> calculated column, complete later"
do
{
$p = Read-Host " Select Primary Name field by index number"
}
while( [int]$p -lt 1 -or [int]$p -gt $nums.count )
$PrimaryColumn[$table["object_id"]] = $(if($nums[[int]$p-1] -eq -1) { -1 } else { $tc[$nums[[int]$p-1]] })
}
$p = @{ TableColumnNames = $TableColumnNames
PrimaryColumn = $PrimaryColumn
}
if( $UsePresets )
{
$p | Export-CliXML $SQLPresets
}
}
#========================================================================================================================
Write-Host "Creating Entities and Columns..."
$CrmColumnTypes = @{}
$entities = @{}
$logicalEntityName = @{}
ForEach($table in $tables)
{
if( ($OneEntity -ne "") -and ($table["name"] -ne $OneEntity) )
{
continue;
}
" {0}" -f $table["name"]
$em = new-object Microsoft.Xrm.Sdk.Metadata.EntityMetadata
$em.SchemaName = SchemaName $table["name"]
$em.LogicalName = LogicalName $table["name"]
$logicalEntityName[$table["name"]] = $em.LogicalName
$em.DisplayName = SingularDisplayName $table["name"]
$em.DisplayCollectionName = PluralDisplayName $table["name"]
$em.OwnershipType = $Ownership
$em.IsActivity = 0
$pn = new-object Microsoft.Xrm.Sdk.Metadata.StringAttributeMetadata
if( $PrimaryColumn[$table["object_id"]] -eq -1 )
{
$name = "Primary Name"
$maxlength = 220
$CrmColumnTypes[-1] = [Microsoft.Xrm.Tooling.Connector.CrmFieldType]::String
}
else
{
$p = $columns[$PrimaryColumn[$table["object_id"]]]
$name = $p["name"]
$maxlength = $p["max_length"]/2
if( !$p["is_nullable"] )
{
$pn.RequiredLevel = new-object Microsoft.Xrm.Sdk.Metadata.AttributeRequiredLevelManagedProperty( [Microsoft.Xrm.Sdk.Metadata.AttributeRequiredLevel]::ApplicationRequired )
}
if( $p["is_identity"] )
{
$pn.AutoNumberFormat = "{SEQNUM:8}"
$maxlength = 8
}
$CrmColumnTypes[$PrimaryColumn[$table["object_id"]]] = [Microsoft.Xrm.Tooling.Connector.CrmFieldType]::String
}
$pn.SchemaName = SchemaName $name
$pn.DisplayName = DisplayName $name
$pn.LogicalName = LogicalName $name
$pn.MaxLength = $maxlength
$cer = new-object Microsoft.Xrm.Sdk.Messages.CreateEntityRequest
$cer.Entity = $em
$cer.PrimaryAttribute = $pn
" {0}" -f $name
$entities[$table["name"]] = XRMExecute $cer
ForEach( $ci in $TableColumns[$table["object_id"]] )
{
$col = $columns[$ci]
if( [String]::IsNullOrEmpty($col["lookup_object_id"].ToString()) -and ($ci -ne $PrimaryColumn[$table["object_id"]]) )
{
switch( $col["type"] )
{
"nvarchar"
{
if( $col["Properties"] -imatch 'axl:texttype="hyperlink"' )
{
$a = New-Object Microsoft.Xrm.Sdk.Metadata.StringAttributeMetadata
$a.MaxLength = 4000
$a.Format = [Microsoft.Xrm.Sdk.Metadata.StringFormat]::Url
$h = [Microsoft.Xrm.Tooling.Connector.CrmFieldType]::String
}
elseif( $col["max_length"] -le 440 -and $col["max_length"] -ge 0 )
{
$a = New-Object Microsoft.Xrm.Sdk.Metadata.StringAttributeMetadata
$a.MaxLength = $col["max_length"] / 2
$h = [Microsoft.Xrm.Tooling.Connector.CrmFieldType]::String
}
else
{
$a = New-Object Microsoft.Xrm.Sdk.Metadata.MemoAttributeMetadata
$a.MaxLength = $MaxMemoLength
$h = [Microsoft.Xrm.Tooling.Connector.CrmFieldType]::String
}
}
"decimal"
{
$a = New-Object Microsoft.Xrm.Sdk.Metadata.DecimalAttributeMetadata
$h = [Microsoft.Xrm.Tooling.Connector.CrmFieldType]::CrmDecimal
}
"int"
{
if( $col["Properties"] -match "axl:image" )
{
$a = New-Object Microsoft.Xrm.Sdk.Metadata.ImageAttributeMetadata
$h = [Microsoft.Xrm.Tooling.Connector.CrmFieldType]::Raw
}
else
{
$a = New-Object Microsoft.Xrm.Sdk.Metadata.IntegerAttributeMetadata
$h = [Microsoft.Xrm.Tooling.Connector.CrmFieldType]::CrmNumber
}
}
"date"
{
$a = New-Object Microsoft.Xrm.Sdk.Metadata.DateTimeAttributeMetadata
$a.Format = [Microsoft.Xrm.Sdk.Metadata.DateTimeFormat]::DateOnly
$h = [Microsoft.Xrm.Tooling.Connector.CrmFieldType]::CrmDateTime
}
"datetime2"
{
$a = New-Object Microsoft.Xrm.Sdk.Metadata.DateTimeAttributeMetadata
$a.Format = [Microsoft.Xrm.Sdk.Metadata.DateTimeFormat]::DateAndTime
$h = [Microsoft.Xrm.Tooling.Connector.CrmFieldType]::CrmDateTime
}
"bit"
{
$a = New-Object Microsoft.Xrm.Sdk.Metadata.BooleanAttributeMetadata
$yes = new-object Microsoft.Xrm.Sdk.Label( "Yes", $lcid )
$no = new-object Microsoft.Xrm.Sdk.Label( "No", $lcid )
$yeso = new-object Microsoft.Xrm.Sdk.Metadata.OptionMetadata( $yes, 1 )
$noo = new-object Microsoft.Xrm.Sdk.Metadata.OptionMetadata( $no, 0 )
$a.OptionSet = new-object Microsoft.Xrm.Sdk.Metadata.BooleanOptionSetMetadata( $yeso, $noo )
$h = [Microsoft.Xrm.Tooling.Connector.CrmFieldType]::CrmBoolean
}
"float"
{
$a = New-Object Microsoft.Xrm.Sdk.Metadata.DoubleAttributeMetadata
$h = [Microsoft.Xrm.Tooling.Connector.CrmFieldType]::CrmFloat
}
default
{
Write-Host "Unknown type: Column " $col["name"] ", " $col["type"]
}
}
$CrmColumnTypes[$ci] = $h
$a.DisplayName = DisplayName $col["name"]
if( $h -eq [Microsoft.Xrm.Tooling.Connector.CrmFieldType]::Raw )
{
$a.SchemaName = SchemaName "EntityImage"
$a.LogicalName = LogicalName "EntityImage"
}
else
{
$a.SchemaName = SchemaName $col["name"]
$a.LogicalName = LogicalName $col["name"]
}
if( !$col["is_nullable"] )
{
$pn.RequiredLevel = new-object Microsoft.Xrm.Sdk.Metadata.AttributeRequiredLevelManagedProperty(
[Microsoft.Xrm.Sdk.Metadata.AttributeRequiredLevel]::ApplicationRequired )
}
$car = new-object Microsoft.Xrm.Sdk.Messages.CreateAttributeRequest
$car.Attribute = $a
$car.EntityName = $em.LogicalName
" {0}" -f $col["name"]
$na = XRMExecute $car
}
}
}
#========================================================================================================================
Write-Host "Creating Relationships..."
$c = 0
ForEach($table in $tables)
{
if( $OneEntity -ne "" )
{
break;
}
$t = 0
ForEach( $ci in $TableColumns[$table["object_id"]] )
{
$col = $columns[$ci]
if( -not [String]::IsNullOrEmpty($col["lookup_object_id"].ToString()) )
{
if( -not $t )
{
" {0}" -f $table["name"]
$t = 1
$c = 1
}
" {0}" -f $col["name"]
ForEach( $r in $tables )
{
if( $col["lookup_object_id"] -eq $r["object_id"] )
{
$l = $logicalEntityName[$r["name"]]
}
}
$la = New-Object Microsoft.Xrm.Sdk.Metadata.LookupAttributeMetadata
$la.DisplayName = DisplayName $col["name"]
$la.LogicalName = LogicalName $col["name"]+"_"+$l
$la.SchemaName = SchemaName $col["name"]+"_"+$l
if( !$col["is_nullable"] )
{
$la.RequiredLevel = new-object Microsoft.Xrm.Sdk.Metadata.AttributeRequiredLevelManagedProperty(
[Microsoft.Xrm.Sdk.Metadata.AttributeRequiredLevel]::ApplicationRequired )
}
$one = New-Object Microsoft.Xrm.Sdk.Metadata.OneToManyRelationshipMetadata
$one.ReferencedEntity = $l
$one.ReferencingEntity = $logicalEntityName[$table["name"]]
$one.SchemaName = $logicalEntityName[$table["name"]]+"_"+$l
$c1m = New-Object Microsoft.Xrm.Sdk.Messages.CreateOneToManyRequest
$c1m.Lookup = $la
$c1m.OneToManyRelationship = $one
$na = XRMExecute $c1m
}
}
}
if( ! $c )
{
" (no relationships)"
}
#========================================================================================================================
"Loading Data..."
for( $retry = 0; ($retry -lt 5) -and (-not $xrm.isready); $retry++ )
{
"Waiting for XRM to be ready..."
Start-Sleep -s 10
}
if( -not $xrm.isready )
{
Write-Host -foregroundcolor Red "Giving up, exiting..."
break
}
$IDMap = @{}
for( $t = 0; $t -lt $LoadOrder.count; $t++ )
{
$table = $tables[$TableIndex[$LoadOrder[$t]]]
if( ($OneEntity -ne "") -and ($table["name"] -ne $OneEntity) )
{
continue;
}
[array]$data = ReadSQL ("select * from Access.[" + $table["name"] + "]")
" {0} ({1} row{2})" -f $table["name"], $data.length, $(if( $data.length -gt 1 ) {"s"} else {""})
$tc = $TableColumns[$table["object_id"]]
$IDMap[$table["object_id"]] = @{}
foreach( $d in $data )
{
$cnr = new-object 'system.collections.generic.dictionary[System.String,Microsoft.Xrm.Tooling.Connector.CrmDataTypeWrapper]'
for( $c = 0; $c -lt $tc.count; $c++ )
{
$column = $columns[$tc[$c]]
# Access Web Apps stored hyperlinks in teh format "DisplayString#URLString#", this extracts the URL
if( $column["Properties"] -imatch 'axl:texttype="hyperlink"' )
{
if( $d[$column["name"]] -match '[^#]*#([^#]+)' )
{
$d[$column["name"]] = $matches[1]
}
}
elseif( ($d[$column["name"]] -isnot [DBNull]) )
{
$ln = LogicalName $column["name"]
$lt = new-object Microsoft.Xrm.Tooling.Connector.CrmDataTypeWrapper
if( $column["lookup_object_id"] -is [DBNull] )
{
$lt.Type = $CrmColumnTypes[$tc[$c]]
if( $lt.Type -eq [Microsoft.Xrm.Tooling.Connector.CrmFieldType]::Raw )
{
$ln = LogicalName "entityimage"
$image = ReadSQLImage -table ("Access.["+$table["name"]+"?images]") -id $d[$column["name"]]
$lt.Value = [System.Byte[]]$image
}
elseif( $lt.Type -eq [Microsoft.Xrm.Tooling.Connector.CrmFieldType]::String )
{
$lt.Value = [string]$d[$column["name"]]
}
else
{
$lt.Value = $d[$column["name"]]
}
$cnr.Add( $ln, $lt )
}
else
{
$lt.Value = $IDMap[$column["lookup_object_id"]][$d[$column["name"]]]
$lt.Type = [Microsoft.Xrm.Tooling.Connector.CrmFieldType]::Lookup
$lt.ReferencedEntity = LogicalName $tables[$TableIndex[$column["lookup_object_id"]]]["name"]
$cnr.Add( $ln, $lt )
}
}
}
$ln = LogicalName $table["name"]
$na = XRMCreateNewRecord $ln $cnr
$IDMap[$table["object_id"]][$d[$identity[$table["object_id"]]]] = $na
}
}
}